SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Contact form

  1. #1
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Contact form

    I have made a contact form the mail to part works the subject works but none of the content is in the email just the content topic lines. Please just give me hints as to what I did wrong so I can figure it out. I need to learn what and why not just how to fix it, so I don't do it again.
    <?php
    //create short variable names
    $fname=$HTTP_post_VARS['fname'];
    $lname=$HTTP_post_VARS['lname'];
    $email=$HTTP_post_VARS['email'];
    $comments=$HTTP_post_VARS['comments'];

    $toaddress = 'me@oursite.com';
    $subject = 'Comments from Our Web-site';
    $mailcontent = 'Customer name: '.$fname.$lname."\n"
    .'Customer Email: '.$email."\n"
    .'Customer Comments: '.$comments."\n";
    $fromaddress = 'From: email';
    mail ($toaddress, $subject, $mailcontent, $fromaddress);

    ?>
    here is the HTML
    <form name="contact_us" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
    <div>
    <label for="fname">First Name/label>
    <input cols="40" type="text" name="fname" id="fname"/>
    </div>
    <br/>
    <div>
    <label for="lname">Last Name/label>
    <input cols="40" type="text" name="lname" id="lname"/>
    </div>
    <br/>
    <div>
    <label for="email">Email/label>
    <input cols="40" type="text" name="email" id="email"/>
    </div>
    <div>
    <label for="comments">Please tell us your comments or questions/label><br/>
    <textarea cols="40" name="comments" rows="10">
    </textarea><br/>
    <input type="submit" value="Tell us"/>
    </div>
    </form>
    Thank-you
    Pat

  2. #2
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code looks alright to me. Btw, use $_POST instead of $HTTP_post_VARS.

    $HTTP_post_VARS is depreciated.
    Create your chat site in 30 seconds!
    Gixaw Chat

  3. #3
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that is what it was $HTTP_post_VARS is depreciated.
    Thats what I get for using an older book.
    Pat

  4. #4
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now this contact form seems to be caught in a loop. I have tested it two times and recieved 12 emails and they are still comming in. Any Ideas why.
    Pat

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if this would solve the repeated sending, but you should only have the PHP code execute when the form is posted, rather than every time the page is loaded. Also, be sure to validate the user's input.

    On a side note, the input text fields use the "size" attribute rather than the "cols" attribute, which is for the textarea tag.

    PHP Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      
    //create short variable names
      
    $fname $_POST['fname'];
      
    $lname $_POST['lname'];
      
    $email $_POST['email'];
      
    $comments $_POST['comments'];

      
    $toaddress 'me@oursite.com';
      
    $subject 'Comments from Our Web-site';
      
    $mailcontent 'Customer name: '.$fname.' '.$lname."\n".'Customer Email: '.$email."\n".'Customer Comments: '.$comments."\n";
      
    $fromaddress 'From: '.$email;
      
    mail ($toaddress$subject$mailcontent$fromaddress);
    } else {
    ?>
    <form name="contact_us" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div>
    <label for="fname">First Name</label>
    <input size="40" type="text" name="fname" id="fname" />
    </div>
    <br />
    <div>
    <label for="lname">Last Name</label>
    <input size="40" type="text" name="lname" id="lname" />
    </div>
    <br />
    <div>
    <label for="email">Email</label>
    <input size="40" type="text" name="email" id="email" />
    </div>
    <div>
    <label for="comments">Please tell us your comments or questions</label><br />
    <textarea cols="40" name="comments" rows="10">
    </textarea><br />
    <input type="submit" value="Tell us" />
    </div>
    </form>

    <?php
    }
    ?>

  6. #6
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank-you for that information.I will let you know if it solves the problem. Well that seemed to take care for the problem. It also really cleaned up the coding for me and gave me the chance to study the new code and see where I was going wrong. Now if you can point me in the direction for up to-date on tutorial on doing validation. The other thing I need on this form is a redirect to a page thanking the customers for using the form.
    Pat

  7. #7
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Next two questions Can I use this
    even though i used _post can I just change that to post. Or is that one only good for the _request type? I know it may be a stupid question But i am just trying to learn if I can take some things in examples like that and change them.
    Pat
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email

  8. #8
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am doing a form I already have it working the way i want but now I want to add validation and spam prevention. My PHP already starts with if so I am not sure how to do this. I have been reading and trying to understand w3schools.com but everything I try does not work. I would also like to find the function for obtaining someone's IP address when they fill out the form. Here is what I have. The form is already posted above.
    Pat

  9. #9
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can search this forum for validation or validate or a search engine for PHP form validation. Basically, for this situation you want to make sure that the input for name is a name and not a number, for example. Or that the e-mail follows the pattern of an e-mail.

    You could also try PHP's Filter Functions. These are for newer versions of PHP, and I haven't had a chance to use them, yet.

    To redirect after the e-mail has been sent use the header function.
    PHP Code:
      mail ($toaddress$subject$mailcontent$fromaddress);
      
    header('Location: http://www.yoursite.com/thankyou.html');
      exit; 

    When a form is submitted the method used is usually POST. Then in your PHP you would use $_POST. Using $_REQUEST allows the value to be from either $_POST, $_GET or $_COOKIE, and the values can be fed to you PHP without having to use your form, which you don't want.

    PHP Code:
    if (isset($_POST['email']))
    #  or
    if (isset($email)) #since you assigned $email the $_POST['email'] value 

  10. #10
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is really causing me a headache I tried... header('Location: http://www.bluegrassmountain.com/thankyou.php');

    exit;
    I use an include for my header and I get the error message
    Warning: Cannot modify header information - headers already sent by (output started at mysite/inc/header.inc.php:9) in mysite/contact.php on line 34

    My email validate does not work either. This is a few lines of it
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    if (isset($_POST['email'])) #Validate email address is included

    //create short variable names

    $fname = $_POST['fname'];

    $lname = $_POST['lname'];
    I know there is something wrong or missing here. But I am just to new to figure this out. I also want to add the spam checker to it. I found the code but cant get it to work either. This is where I get confused putting functions together.
    Pat

  11. #11
    SitePoint Addict Kysmiley's Avatar
    Join Date
    Dec 2004
    Location
    Isonville KY
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know this is simple, I just cant find the answer I have one <?php how do I place one inside another.
    here is what I am trying,
    mail ($toaddress, $subject, $mailcontent, $fromaddress);
    header('Location: http://www.yoursite.com/thankyou.html');
    exit;
    when I try to use the redirect it does not work either it gives me the error message about the header


    } else {



    echo " ?>
    <center><h2>Please use our contact form to ask us questions or give comments...</h2></center>

    <form name="contact_us" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <div>

    <label for="fname">First Name</label>
    and at the bottom after the form and other text";
    </php
    }
    ?>
    Ok So where am I going wrong. What am I not understanding.

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    The headers need to be set before you output anything to the browser. If a single line is outputted before setting the header your going to get the header error.

  13. #13
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be some output in the header.inc.php file. If you are having trouble resolving this, please post all the page code.

    Also, you don't need "echo" to output the html in the else condition
    PHP Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    # stuff
    } else {
    ?>
    <center><h2>Please use our contact form to ask us questions or give comments...</h2></center>

    <form name="contact_us" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <div>

    <label for="fname">First Name</label>
    and at the bottom after the form and other text
    <?php
    }
    ?>


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
  •