SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    UK
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Contact Form Help

    I have put a contact form on my website and when filling in a test message it comes through to my email correctly saying a message has been received from site visitor but the senders name/email/message is blank.

    I cant work out what I have missed on the coding. Could anyone kindly take a look and help me out please.

    The contact form is on this page www.bodybusterfitness.co.uk/contact.htm and the code file is www.bodybusterfitness.co.uk/contact.php

    Many thanks in advance

  2. #2
    SitePoint Member davidclark's Avatar
    Join Date
    Apr 2014
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gooders2274 View Post
    I have put a contact form on my website and when filling in a test message it comes through to my email correctly saying a message has been received from site visitor but the senders name/email/message is blank.

    I cant work out what I have missed on the coding. Could anyone kindly take a look and help me out please.

    The contact form is on this page www.bodybusterfitness.co.uk/contact.htm and the code file is www.bodybusterfitness.co.uk/contact.php

    Many thanks in advance
    I look at your HTML code and I see this
    Code:
    <p>Name<br> <input type="text" name="name" style="width: 300px">
    <p>Contact Number<br> <input type="text" name="name" style="width: 300px">
    <p>Email Address<br> <input type="text" name="email"  style="width: 300px">
    
    <p>How did you hear about us?<br><textarea name="message" rows="6" cols="25"  style="width: 300px">
    </textarea>
    <br><br>
    <b>If you would like to attend camp with a friend please provide their name and contact number below</b>
    <br>
    <p>Name<br> <input type="text" name="name" style="width: 300px">
    <p>Contact Number<br> <input type="text" name="name" style="width: 300px">
    As you see the problem come from "name", you put the name of many input as "name" so that the server code can't receive data, you need to have a unique name for each of them.

  3. #3
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,200
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    If the above fix doesn't resolve the issue (though it is important), you'll have to show us the contents of the PHP file.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    UK
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for getting back to me. I've tried changing "name" on all the fields and adding something personalised for each line but I'm not having any luck.

    This is the HTML of my form;

    Code HTML4Strict:
    <form action="contact.php" method="POST">
     
    <p>Name<br> <input type="text" name="name" style="width: 300px">
    <p>Contact Number<br> <input type="text" name="phone" style="width: 300px">
    <p>Email Address<br> <input type="text" name="email"  style="width: 300px">
     
    <p>How did you hear about us?<br><textarea name="message" rows="6" cols="25"  style="width: 300px">
    </textarea>
    <br><br>
    <b>If you would like to attend camp with a friend please provide their name and contact number below</b>
    <br>
    <p>Name<br> <input type="text" name="friend_name" style="width: 300px">
    <p>Contact Number<br> <input type="text" name="friend_phone" style="width: 300px">
    </textarea><br />
     
    <input type="submit" value="Send"><input type="reset" value="Clear">
     
    </form>


    and this is the PHP file contents

    Code PHP:
    <?php
    $field_name = $_POST['cf_name'];
    $field_email = $_POST['cf_phone'];
    $field_email = $_POST['cf_email'];
    $field_message = $_POST['cf_message'];
    $field_name = $_POST['cf_friend_name'];
    $field_email = $_POST['cf_friend_phone'];
     
    $mail_to = 'myself@example.com';
    $subject = 'Message from a site visitor '.$field_name;
     
    $body_message = 'From: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Message: '.$field_message;
     
    $headers = 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";
     
    $mail_status = mail($mail_to, $subject, $body_message, $headers);
     
    if ($mail_status) { ?>
    	<script language="javascript" type="text/javascript">
    		alert('Thank you for the message. We will contact you shortly.');
    		window.location = 'thank_you.htm';
    	</script>
    <?php

    Thanks in advance for your help its much appreciated.
    Last edited by TechnoBear; May 7, 2014 at 06:15. Reason: Code tags added & example e-mail address substituted

  5. #5
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,200
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    The names in your form code need to match those post values in the PHP code. So for the name input, you should have name="cf_name" etc.

    Also, each of the variable names in the PHP code must be unique, so you cannot use $field_name twice, for example. For the friend name, use something like $friend_name etc. You will also need to add extra $body_message lines to include these in the email.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    UK
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the replies. I've made some changes and its still not working. its probably just a full stop or comma missing somewhere, if anyone can see what I'm missing please let me know

    HTML Code


    Code HTML4Strict:
    <form action="contact3.php" method="POST">
     
    <p>Name<br> 
    <input type="text" name="name" style="width: 300px">
    <p>Contact Number<br> <input type="text" name="phone" style="width: 300px">
    <p>Email Address<br> <input type="text" name="email"  style="width: 300px">
     
    <p>How did you hear about us?<br><textarea name="message" rows="3" cols="25"  style="width: 300px">
    </textarea>
    <br><br>
    <b>If you would like to attend camp with a friend please provide their name and contact number below</b>
    <br>
    <p>Name<br> <input type="text" name="friend_name" style="width: 300px">
    <p>Contact Number<br> <input type="text" name="friend_phone" style="width: 300px">
    </textarea><br />
     
    <input type="submit" value="Send"><input type="reset" value="Clear">
     
    </form>

    PHP Code

    Code PHP:
    <?php
    $field_name = $_POST['cf_name'];
    $field_phone = $_POST['cf_phone'];
    $field_email = $_POST['cf_email'];
    $field_message = $_POST['cf_message'];
    $field_friend_name = $_POST['cf_friend_name'];
    $field_friend_email = $_POST['cf_friend_phone'];
     
    $mail_to = 'myself@example.com';
    $subject = 'Message from a site visitor '.$field_name;
     
    $body_message = 'From: '.$field_name."\n";
    $body_message .= 'Phone: '.$field_phone."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Message: '.$field_message."\n";
    $body_message .= 'Friend-Name: '.$field_friend_name."\n";
    $body_message .= 'Friend-Phone: '.$field_friend_phone;
     
    $headers = 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";
     
    $mail_status = mail($mail_to, $subject, $body_message, $headers);
     
    if ($mail_status) { ?>
    	<script language="javascript" type="text/javascript">
    		alert('Thank you for the message. We will contact you shortly.');
    		window.location = 'thank_you.htm';
    	</script>
    <?php
    }
    else { ?>
    	<script language="javascript" type="text/javascript">
    		alert('Message failed. Please, send an email to [email]me@mydomain.co.uk[/email]');
    		window.location = 'contact.htm';
    	</script>
    <?php
    }
    ?>

    This is what I receive in the email from a site visitor

    From:
    Phone:
    E-mail:
    Message: Message: Message: Friend-Name:
    Friend-Phone: From:


    Many thanks for your help
    Last edited by TechnoBear; May 7, 2014 at 06:17. Reason: Code tags added & example e-mail address substituted

  7. #7
    SitePoint Member davidclark's Avatar
    Join Date
    Apr 2014
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gooders2274 , your PHP code is still a wrong param name in $_POST to receive value from form. It must be

    Code:
    $field_name = $_POST['name'];
    not

    Code:
    $field_name = $_POST['cf_name'];
    To know what you can receive in PHP file, I suggest you can put this line in the first line of PHP file

    Code:
    <?php
    echo "<pre>";print_r($_POST);echo "</pre>";
    It 's a basic way to debug PHP and it will show you all of key and value you can receive from form.

    Good luck again

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    UK
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much this worked. The only thing I am not receiving on the email is the last field 'friends phone number'
    I have tried ending the field in 2 ways and its still blank;

    $body_message .= 'Friend-Phone: '.$field_friend_phone."\n";
    and
    $body_message .= 'Friend-Phone: '.$field_friend_phone;

    Any ideas please?

  9. #9
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,200
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    You'll need to show us the code you have now, since the code you last showed us still had the problems we addressed earlier.

    In the initial lines of PHP, you have this:

    $field_friend_name = $_POST['cf_friend_name'];
    $field_friend_email = $_POST['cf_friend_phone'];

    There's no mention of $field_friend_phone. You probably need to change that second line to

    $field_friend_phone = $_POST['cf_friend_phone'];

    But also make sure that the name="" value in the form matches the value in blue above. Call it whatever you like, but they must be the same.


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
  •