You can combine HTML and PHP to create a form on your website, whether you need a contact form, feedback survey, or order form. A form captures the data entered and sends it to a specified email address.
In this guide, we will walk you through the steps to configure and implement a contact form on your website.
Note: The following piece of code is only an example of how the form could be used.
Make corrections to the different fields to match your website.
- Step 1 - Add the code for the form to the PHP file
- Step 2 - Change and customise the form
- Step 3 - Save the changes and upload the PHP file
- Step 4 - Test the form
Step 1 - Add the code for the form to the PHP file
- Using a text editor or an HTML editor, open the PHP file where you want to add the form.
Tip: If it’s an HTML file, you can easily transform it into a PHP file by simply changing the file extension. For example, if the file is called contact.html, renaming it to contact.php is sufficient.
-
Copy the provided code and paste it into the desired location within the PHP file. Ensure the form is placed within the
<body>
section of the PHP document.
Click to reveal the code that you can insert on your website:
-
Code for the form
<?php
// if "email" variable is filled out, send email
if (isset($_REQUEST['email'])) {
//Email information
$to = "email@example.com";
$subject = "Message from website";
$message = "From: " . $_REQUEST['name'] . "\r\n" .
"Email: " . $_REQUEST['email'] . "\r\n" .
"Message:" . $_REQUEST['message'];
$headers = 'From: email@example.com' . "\r\n" .
'Reply-To: email@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//Send email
mail($to, $subject, $message, $headers);
//Email response
echo "Thank you for contacting us!"; }
//if "email" variable is not filled out, display the form
else { ?>
<form method="post" >
<label for="name">Name* </label><br />
<input name="name" required="true" /><br />
<label for="email">Email* </label><br />
<input name="email" required="true" /><br />
<label for="message">Your message*</label><br />
<textarea cols="100" rows="10" name="message" required="true"></textarea><br />
<p><small>Required fields</small></p>
<input type="submit" value="Send" />
</form>
<?php } ?>
-
Step 2 - Change and customise the form
Necessary changes to the form:
line 6 → This is going to be the recipient of our email.
line 13 → This is going to be the sender.
Note: It needs to be an email address on your domain. So if, for example, you have the domain one-example.com, it needs to be a @one-example.com address.
line 14 → This is the reply-to field.
Customization and optional changes to the form:
line 7 → This is the subject.
line 21 → This is the thank you message.
lines 27, 30, 33 → These are the messages above the fields.
line 36 → This can be any message that is displayed below the form.
line 38 → This is the text for the button.
lines 2, 5, 17, 20, 23 can be deleted if needed. They are just comments.
Step 3 - Save the changes and upload the PHP file
- Save the changes to the PHP file after adding the form code.
- Upload the updated PHP file to the web server using an SFTP client or one.com's File Manager.
Step 4 - Test the form
Open the webpage in a browser and test the form to ensure it works correctly. Fill in the form fields and submit it to verify that the data is sent to the specified email address.
Related articles: