In a previous post, we discussed how to validate information entered into an HTML contact form. Now, processing an HTML form with PHP will allow you to do something with that data. There are a variety of ways for this action to handled, but for this example, we are going to assume that you want to have the information emailed to you, and that you would like the user to be redirected to a thank you page.
The Contact Form
In case you didn’t read the last post, here is the HTML for your form:
You’ll notice that the action for this form is set to
form_handler.php. This is the file that you are going to create to handle your form. Create a blank PHP file and save it as
form_handler.php and make sure that it is in the root folder of your website (you can place it elsewhere, but make sure to change the
action of the script to include the correct file path).
All-new Echo (2nd Gen) has a new speaker, new design, and is available in a range of styles including fabrics and wood veneers. Echo connects to Alexa to play music, make calls, set music alarms and timers, ask questions, control smart home devices, and more—instantly.
Processing the Form
Once your file has been created, insert the following code:
That’s it! What’s great about this script is that is doesn’t matter how many fields you have in your form, the script will loop through all of them and output that information into the email (this includes all the
input types as well as
There are a few things that you’ll want to customize for use on your own website. First, the variable
$subject will be the subject line of the email that is sent to you. Second, the variable
$mailto: will be the email address to which you would like the email to be sent (presumably your own unless you are using this script for a third-party website). Finally, the line
header('Location: http://www.example.com/thank-you') handles the redirect for a successful submission. Change this to the url of the page you wish to send your users to once they have submitted a form. Be sure to leave the
Location: segment in place for the redirection will not work correctly.