Forms in PHP are ways to collect information from users and process it .This data can be stored in database ,display it on screen or we can simply mail the details to admin user.In this tutorial we are going to create a form which will collect information and send email to admin.
Prerequisites
- Basic PHP Knowledge
2. Basic Html Knowledge
Form has html inputs like text ,text areas,select,radio buttons,checkboxes and button.
General Flow of Form Screenshot:

Create a php page index.php in the folder c:\xampp\htdocs\phpforms folder.Paste the below html structure for form in the php page
.Check the code below for html structure for form:
<form action="process.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<input type="submit" value="Submit">
</form>
In the above code we have used the below:
<form></form> : form tag creates a form container.In this container the fields has been defined.
action=”process.php” : The action or location where form data is submitted .The data gets processed here.
method=”POST” : Method by which form data is submitted.This the most secure way of submitting data.
<input> elements : Fields for text and email and submit button.
How Data Is Sent To Server
See the below screen which we get after adding form structure to a PHP page.

Whenuser clicks SUBMIT button the data is submitted to server using action and method attributes of form.The data is sent to process.php.It is then proceesed and is sent to mail .
PHP Retrieving Form Data
In process.php the two variables are fetched ie name and email.The data for two fields are fetched using $_POST superglobal array.Here$_POST is used as we have sent data using POST method.After data is stored in two variables we use mail function to send the details to admin.
mail() is the PHP function which is used to send emails.PHP uses PHPMailer or SMTP to send email.PHPMailer is the default feature. But if you need to send it through SMTP you need to set SMTP details in your server.
Check the below code for processing the submitted form data and send email to admin
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars(trim($_POST['name']));
$email = htmlspecialchars(trim($_POST['email']));
if (!empty($name) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Send email
$to = "admin@example.com";
$subject = "New Contact Form Submission";
$message = "Name: $name\nEmail: $email";
$headers = "From: $email";
if (mail($to, $subject, $message, $headers)) {
echo "Message sent successfully!";
} else {
echo "Failed to send message.";
}
}
}
?>
if ($_SERVER["REQUEST_METHOD"] == "POST") {
The above line of code checks if the request method used to submit data is POST. If method is POST ,the code that follows gets executed.
$name = htmlspecialchars(trim($_POST['name']));
$email = htmlspecialchars(trim($_POST['email']));
In the above code we have stored name and email values in 2 respective $name and $email fields.
I used the trim() function to remove extra spaces from the values.
The htmlspecialchars() function prevents XSS attacks by escaping special characters in user input
if (!empty($name) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
In the above line of code, a condition checks if the email is in the proper format; otherwise, the code below will not execute.
$to = "admin@example.com";
$subject = "New Contact Form Submission";
$message = "Name: $name\nEmail: $email";
$headers = "From: $email";
if (mail($to, $subject, $message, $headers)) {
echo "Message sent successfully!";
} else {
echo "Failed to send message.";
}
In the above code you set the subject,headers and message for sending email.
Then the mail() function sends the email to admin.
Congratulations!You have created a contact form with name and email. fields .With this you have now complete knowledge of how to create a form and it’s processing.
In this guide you learned the complete lifecycle of form and it’s processing.
happy Coding!