PHP form > PHP forms tutorial >
Putting it all together
Final PHP contact form
Here we will put together all we learned in this PHP forms tutorial
and create a working PHP contact form.
» HTML form code
Let's use the form we started with this tutorial and just add a few more fields
to make it more interesting. In this example we will make fields "Your name",
"Subject", "E-mail" and "Comments" required, all others optional.
We will mark required field labels bold so the visitor knows which
fields he/she has to fill in.
Copy the HTML code below it into
a plain text file and save it as contact.htm
<html>
<body>
<p>Required fields are <b>bold</b></p>
<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>
<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
<p> </p>
<p>Powered by <a href="https://myphpform.com">PHP form</a></p>
</form>
</body>
</html>
|
» Thank you page
We could include the response in the PHP script (as shown before),
but keeping it in an outside file makes the script itself less
complicated and the response page easier to edit and customize.
Copy the HTML code below it into a plain text file and save it as thanks.htm
<html>
<body>
<p><b>Your message was sent</b></p>
<p>Your message was successfully sent!
Thank you for contacting us, we will reply
to your inquiry as soon as possible!</p>
</body>
</html>
|
» PHP form script
This script is just a summary of topics covered in this tutorial.
Included are some comments to explain what is happening.
Copy the PHP code below it into a plain text file and save it as contact.php
Change the default "you@domain.com" recipient address
inside the code to your own e-mail address (the one you wish to receive
form results to)!
<?php
/* Set e-mail recipient */
$myemail = "you@domain.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
|
» Getting it to work
Once you have all the three files (contact.htm, thanks.htm and contact.php)
upload them to your server (check with your host and make sure it
supports PHP!), open contact.htm in your browser and give it a try.
Not receiving e-mails from your form? See
PHP form not working.
Congratulations! Your PHP contact form is now up and running!
» Copyright notice
© 2008-2024 myPHPform.com. All rights reserved. Copying or redistributing
any part of this website without our written permission is expressly
forbidden!
|