How do I create a form with PHP?
Important: If you received our email about updating website forms using FormMail.pl, please follow this guide to replace the old script with a modern, secure alternative before November 30, 2025. If your site has multiple forms using FormMail.pl, each form needs to be updated.
A form allows visitors to send information directly to your email address, whether it's a contact form, feedback survey, or order form. In this guide, we walk you through creating one using PHP and how you can combine HTML and PHP script.
Note: The code used in this guide is only an example of how the form could be used.
Adjust the different fields and email addresses to match your website.
- Step 1 - Add the form code to the PHP file
- Step 2 - Change and customise the form code
- Step 3 - Save and upload the changes
- Step 4 - Test the form
Step 1 - Add the form code to the PHP file
-
Open the PHP file where you want to add or edit the form. Use a text editor, e.g. Notepad (Windows) or TextEdit (macOS), or an HTML editor, e.g. Visual Studio Code.
Tip: If your current form is on an .html file, you can easily convert it to 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.
Renaming the file changes the page URL, remember to update any links that point to this page to avoid broken links (404 errors).
-
Copy the provided code below 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 = "recipient@example.com";
$subject = "Message from website";
$message = "From: " . $_REQUEST['name'] . "<br>" .
"Email: " . $_REQUEST['email'] . "<br>" .
"Message:" . $_REQUEST['message'];
$headers = [
'From' => 'sender@example.com',
'X-Sender' => 'sender@example.com',
'X-Mailer' => 'PHP/' . phpversion(),
'X-Priority' => '1',
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=utf-8'
];
//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 code
Necessary changes to the form:
line 6 → Replace the email address here with the email address where you want to receive form submissions.
line 14 → This is the sender email address. This must be an email address on your domain. For example, for the domain one-example.com, it needs to be a @one-example.com address.
line 15 → This is the email address where replies will go to.
Tip: We recommend to keep these email addresses the same to avoid emails ending in spam.
Customisation and optional changes to the form:
line 7 → This is the subject line of the email.
line 26 → This is the thank you message to the sender of the form.
lines 32, 35, 38 → This is the text above the input fields. To indicate that the field is required, you have to manually add the asterisk (*).
In the lines below, line 33, 36 and 39, is the code what it actually makes it required. Change "true" to "false" to make the field not mandatory:
required="true"
line 41 → This text is displayed below the form and can be any message, usually used to refer to the asterix (*) for required input fields (in our example, lines 32, 35 and 38).
line 43 → This is the text for the button.
lines 2, 5, 22, 25, 28 are just comments and can be deleted if needed.
Step 3 - Save and upload the changes
- Save the changes to the PHP file after adding or editing the code of the form.
- Upload the updated PHP file to the webspace using an SFTP client or one.com's File Manager.
Step 4 - Test the form
To ensure the new form works correctly, open your website in a browser and go to the page with your contact form. Fill in the form with test information and send it using the button. Check the specified email address for the form to confirm that the test message arrives.
Related articles: