Simple PHP Contact Form
A simple PHP contact form
The first step is to add the form to a HTML page, its needs to be added within the <body> section of a page.
<form action="email.php" method="post"> Email: <input name="email" size="30" type="text" /> Message: <textarea cols="40" rows="15" name="message"> </textarea> <input type="submit" /> </form>
This code creates an input box for the user to put his e-mail address in and the textarea is a message for the user to put his message. The <input type=”submit” /> creates a button for the user to press once the form has been completed. This then activates the script email.php and the info is sent to your chosen e-mail address.
The PHP Script
Next we need to code the PHP Script, make a file called email.php and copy the following into it:
When the form is submitted to email.php, the contents of the email field is put into a PHP variable called $_REQUEST['email'].
The content of the message field is put into a variable called $_REQUEST['message'].
$email = $_REQUEST['email'] ;
You didn’t need to name these $email – we could have called it $field1 but it makes it easier to come back and edit if you name it some relevant hence $email
mail( "email@example.co.uk", " Form Results", $message, "From: $email" ); <span style="font-family: 'Lucida Grande'; line-height: 19px; white-space: normal;">This section tells the form where to send the info email@example.co.uk and the second parameter is the subject of the e-mail “Form Results”.</span>
$message, this tells the script to insert the message in the content of the e-mail.
“From: $email”, this fills in the from field on the e-mail you receive giving you something to reply to.
header( "Location: http://www.example.co.uk/thankyou.html" );
This line causes the browser to redirect the user to page to thank them for filling in the form. You could just re-direct them to the homepage but I like to thank my users for filling in the form
If you enjoyed this post you might enjoy these:





Leave a Comment