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.
<p>Name<br> <input type="text" name="[B]name[/B]" style="width: 300px">
<p>Contact Number<br> <input type="text" name="[B]name[/B]" 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="[B]name[/B]" style="width: 300px">
<p>Contact Number<br> <input type="text" name="[B]name[/B]" 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.
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;
<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
<?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."\
";
$body_message .= 'E-mail: '.$field_email."\
";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\\r\
";
$headers .= 'Reply-To: '.$field_email."\\r\
";
$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.
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.
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
<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
<?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."\
";
$body_message .= 'Phone: '.$field_phone."\
";
$body_message .= 'E-mail: '.$field_email."\
";
$body_message .= 'Message: '.$field_message."\
";
$body_message .= 'Friend-Name: '.$field_friend_name."\
";
$body_message .= 'Friend-Phone: '.$field_friend_phone;
$headers = 'From: '.$field_email."\\r\
";
$headers .= 'Reply-To: '.$field_email."\\r\
";
$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
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."
";
and
$body_message .= 'Friend-Phone: '.$field_friend_phone;