SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict avstu's Avatar
    Join Date
    May 2005
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My PHP contact form

    I have just made myself a very simple contact form. But now i want to move it onto the next level. Firstly here is my coding:

    I have two files

    Contact.php:

    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
    <tr>
    <td><form name="form1" method="post" action="send_contact.php">
    <table width="100&#37;" border="0" cellspacing="1" cellpadding="3">
    <tr class="textclaret">
    <td width="16%">Subject</td>
    <td width="2%">/td>
    <td width="82%"><div align="left">
    <input name="subject" type="text" id="subject" size="50">
    </div></td>
    </tr>
    <tr class="textclaret">
    <td>Detail</td>
    <td>/td>
    <td><div align="left">
    <textarea name="detail" cols="38" rows="4" id="detail"></textarea>
    </div></td>
    </tr>
    <tr class="textclaret">
    <td>Name</td>
    <td>/td>
    <td><div align="left">
    <input name="name" type="text" id="name" size="50">
    </div></td>
    </tr>
    <tr class="textclaret">
    <td>Email</td>
    <td>/td>
    <td><div align="left">
    <input name="customer_mail" type="text" id="customer_mail" size="50">
    </div></td>
    </tr>
    <tr class="textclaret">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><div align="left">
    <input type="submit" name="Submit" value="Submit">
    <input type="reset" name="Submit2" value="Reset">
    </div></td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
    Send_Contact.php
    <?php
    // Contact subject
    $subject ="$subject";
    // Details
    $message="$detail";

    // Mail of sender
    $mail_from="$customer_mail";
    // From
    $header="from: $name <$mail_from>";

    // Enter your email address
    $to ='avstu@avillafan.com';

    $send_contact=mail($to,$subject,$message,$header);

    // Check, if message sent to your email
    // display message "We've recived your information"
    if($send_contact){
    echo "Your Email has been sent!";
    }
    else {
    echo "ERROR";
    }
    ?>

    Now there are a few changes i would like to change but im unsure how to them.

    Firstly, i would like to change the subject bar to a drop down menu.

    Secondly, How do i make fields mandutary so they have to be filled in.

    Thirdly, To avoid spamming, how would my script send a email to the email address filled in the form to validate it. Only once the link in the email is clicked, will the email be sent to me.


    How would i do these? Please bare in mind im very basic with PHP. So no technical terms please

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by avstu View Post
    <td width="2%">/td>
    Look out, someone's eating your td's!

  3. #3
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To change the subject to a drop down:

    Code:
    <select name="subject" >
      <option value ="one">Subject One</option>
      <option value ="two">Subject Two</option>
      <option value ="three">Subject Three</option>
      <option value ="four">Subject Four</option>
    </select>
    to make fields mandatory you can do something like this in you send.php script

    Code:
    if($_POST['name of field to validate'] ==" "){
    echo "a valid field is required";
    
    }
    The following link is a pretty good tutorial for learning the basics of php contact forms.

    http://www.kirupa.com/web/php_contact_form.htm

  4. #4
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    Look out, someone's eating your td's!
    Lol! Good old Pacman to save the day.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  5. #5
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pacman

    Sorry guys that was me.(I eat tables for lunch )

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    To make fields mandetory, you can validate them from both the javascript and from the php.

    It's best to do it first from javascript so that the user doesn't have to fill in the form again.

    A simple example is

    Code HTML4Strict:
    <form name="form1" method="post" action="send_contact.php" onsubmit="return checkform(this)">

    Code JavaScript:
    function checkform (el) {
      var name = el['name'];
      if (name.value) == "") {
        alert( "Please enter your name" );
        name.focus();
        return false;
      }
      return true;
    }
    function trim(str) {
      return str.replace(/^\s*|\s*$/g,'');
    }

    Then the php can have a similar check.

    Code PHP:
    if (isset($_POST['textfieldname']) && trim($_POST['textfieldname']) = '') {
      // he's been bad, so take him back
    }

    You will need to think about how you want to notify the user about the missing fields.

  7. #7
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To add to what pmw57 said, I would make sure that the validation works perfectly client-side(your php) first before adding the client side (javascript) validation. that way if your user has javascript turned off, the form will work flawlessly. What we call graceful degradation, i think

    My preferred way of notifying users of validation errors is to add "required" and "error" classes to your css so that either php or javascript can turn them on or off in case of an error.

    I'll post the code to the contact form on my website if I get time tonight. It makes an ajax call to the contact script so I'll try to only include the parts that are reveleant to your situation without complicating it too much.

    you can check out the form at Quo Vadimus Interactive

    go ahead, try it with javascript turned off.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That's a good point that vidalstat made. Javascript should never be relied on, otherwise you're in the trouble when it's not there.

    Javascript should be used to provide progressive enhancement to a site that's working already without it.

  9. #9
    SitePoint Addict avstu's Avatar
    Join Date
    May 2005
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vidalstat View Post
    To change the subject to a drop down:

    Code:
    <select name="subject" >
      <option value ="one">Subject One</option>
      <option value ="two">Subject Two</option>
      <option value ="three">Subject Three</option>
      <option value ="four">Subject Four</option>
    </select>
    to make fields mandatory you can do something like this in you send.php script

    Code:
    if($_POST['name of field to validate'] ==" "){
    echo "a valid field is required";
    
    }
    The following link is a pretty good tutorial for learning the basics of php contact forms.

    http://www.kirupa.com/web/php_contact_form.htm


    So to add them - what would i replace them in my original coding?

  10. #10
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php contact form

    You would replace the foloowing line:

    Code:
    <input name="subject" type="text" id="subject" size="50">
    with the select tag. Make sure to give your select tag the name="subject" attribute and then you can acccess it with

    Code:
    $_POST['subject'];
    in your processing form.

    Btw, you really don't need to lay this out with a table, do you?

  11. #11
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If you want to learn how to create a contact form, i'd suggest learning all the baggage that comes with it properly.

    1) So to begin, drop your entire table layout, learn to use a HTML list with label.
    HTML Code:
    <form>
        <ul>
            <li><label></label><input></li>
            <li><label></label><input></li>
            </ul>
    </form>
    This is a starting structure, of course you need to add attributes to 'em. Don't use inline styling as tempting as they are.


    2) If you want a required form, there's other things that come with it. Such as refilling in the form with the old information.
    So in Send_Contact.php, u'd need something like this:
    PHP Code:
    session_start();

    # validate input
    foreach ($_POST as $k=>$v) {
        
    $clean[$k] = htmlentities(trim($v), ENT_QUOTES);
    }

    $req = array ('name','email','subject','message');

    foreach (
    $req as $v)
    {
        if (!
    $clean[$v]) # Didn't fill in $v
        # for short forms like this, make it easy on yourself and just do $error = 1; break; and tell the user, please fill in all the fields. 
        # For long forms you'd want to store every invalid field in an array and then tell the user these fields need to be corrected. 
    }

    if (
    /* no error */) {
        
    # send email
    } else {
        
    $_SESSION['contact_data'] = $clean;
        
    header ("Location: Contact.php"); exit;

    This will clean and then check to make sure the required fields are filled in, and if not, send them back to contact.php with the submitted information back.

    And ur input field would look like this now with a message at the top
    if (count($_SESSION['contact_data'])) {
    echo "Please fill in all the required fields.";
    }
    HTML Code:
    <form>
        <ul>
             <li><label><input value="<?=$_SESSION['contact_data']['field_key']?>" /></li>
        </ul>
    </form>
    And at the bottom of Contact.php, u'd want to delete the session data so it doesn't rewrite to it again.

    unset($_SESSION['contact_data']);


    Hope this helps alittle.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •