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

I look at your HTML code and I see this

<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.

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.

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

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

Many thanks for your help

gooders2274 , your PHP code is still a wrong param name in $_POST to receive value from form. It must be

$field_name = $_POST['name'];

not

$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

<?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 :slight_smile:

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;

Any ideas please?

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.