Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP contact form help!

Featured Replies

I am having trouble with my contact form and need some assistance. The form works and the PHP code sends me an email, but the information is not shown on the email, just the field names. Which part of the PHP code will fix this? I have very little knowlegde when it comes to PHP, so I don't have a clue! Thanks!

 

HTML Contact Form

<style type="text/css">
<!--
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style>

<form action="sendmail.php" method="post" name="form" id="form">
 <h2 class="style2"><img src="Images/formtitle.jpg" width="357" height="52" hspace="9" vspace="1" /></h2>
<ul>

		<label><span class="style2">Name</span></label><br />
		<input type="text" class="style2" id="Name" />
           <br />
           <br />
           <label><span class="style2">Email</span></label><br />
		<input type="text" class="style2" id="Email" />
           <br />
           <br />
           <label><span class="style2">Message</span></label>
           <br />
		<textarea cols="40" rows="6" class="style2"></textarea>
		<br />
		<br />
		<p><input name="submit" type="submit" class="style2" value="Submit">
</ul>
</form>

 

PHP Code

<?php

$emailSubject = 'Funky Favours Enquiry';
$webMaster = 'example@hotmail.com';

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Message: $message <br>
EOD;

$headers = "From: $email\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

$theResults = <<<EOD

<html>
<style type="text/css">
<!--
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style>
<form method="post" action="sendmail.php">

<form id="form">
<h2 class="style2"><img src="Images/formtitle.jpg" width="357" height="52" hspace="9" vspace="1" /></h2>
<ul>
	    <label><span class="style2">Name</span></label><br />
		<input type="text" class="style2" id="Name" />
           <br />
           <br />
           <label><span class="style2">Email</span></label><br />
		<input type="text" class="style2" id="Email" />
           <br />
           <br />
           <label><span class="style2">Message</span></label>
           <br />
		<textarea cols="40" rows="6" class="style2"></textarea>
		<br />
		<br />
		<p><input name="submit" type="submit" class="style2" value="Submit">
</ul>

</form>
</html>
EOD;
echo"$theResults";
?>

 

Thanks :D

Give each of your form input fields a name attribute the same as the id atribute .eg

<input type="text" class="style2" name="Email" id="Email" />

In your php script change the following:

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Message: $message <br>
EOD;

 

to

$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
EOD;

  • Author

Code is still not working, there is no information inside the email from the form. I have changed the code to;

 

HTML Code

<style type="text/css">
<!--
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style>

<form action="sendmail.php" method="post" name="form" id="form">
 <h2 class="style2"><img src="Images/formtitle.jpg" width="357" height="52" hspace="9" vspace="1" /></h2>
<ul>

		<label><span class="style2">Name</span></label><br />
		<input type="text" class="style2" name="Name" id="Name" />
           <br />
           <br />
           <label><span class="style2">Email</span></label><br />
		<input type="text" class="style2" name="Email" id="Email" />
           <br />
           <br />
           <label><span class="style2">Message</span></label>
           <br />
		<textarea cols="40" rows="6" class="style2" name="Message" id="Message"></textarea>
		<br />
		<br />
		<p><input name="submit" type="submit" class="style2" value="Submit">
</ul>
</form>

 

PHP Code

<?php

$emailSubject = 'Funky Favours Enquiry';
$webMaster = 'kay346@hotmail.com';

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];

$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
EOD;

$headers = "From: $email\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

$theResults = <<<EOD

<html>
<style type="text/css">
<!--
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style>
<form method="post" action="sendmail.php">

<form id="form">
<h2 class="style2"><img src="Images/formtitle.jpg" width="357" height="52" hspace="9" vspace="1" /></h2>
<ul>
	    <label><span class="style2">Name</span></label><br />
		<input type="text" class="style2" id="Name" />
           <br />
           <br />
           <label><span class="style2">Email</span></label><br />
		<input type="text" class="style2" id="Email" />
           <br />
           <br />
           <label><span class="style2">Message</span></label>
           <br />
		<textarea cols="40" rows="6" class="style2"></textarea>
		<br />
		<br />
		<p><input name="submit" type="submit" class="style2" value="Submit">
</ul>

</form>
</html>
EOD;
echo"$theResults";
?>

 

Were am I going wrong??? Thanks

Change the following

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];

 

to

$nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$messageField = $_POST['Message'];

  • Author

I managed to fix the PHP code and make it work eventually. Turned out PHP was throwing a wobbler over spaces and punctuation. Thanks for all the help! :D

Hey, i have choose to use this form and all seems fine apart from on submision i get the following error.

 

Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 80 in /home/sites/mywebsite.co.uk/public_html/me.php on line 47

 

Here is line 47

 

} elseif (preg_match($profanity, $value) || (preg_match($spamwords, $value)) {

 

 

Any suggestions?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.