How to Create Contact Form with PHP | PHP Email Contact Form
Hi to all, in this article I am going to tell you all about how to create contact form by using PHP code easily in just few minutes.
So, read this article carefully and completely to get clear understanding about creating contact or email form by using PHP and CSS code without any error or issues.
Mainly I will tell you, how to send email using PHP form submit.
And here we are using PHP to send email to the admin email address and design the form by using HTML and CSS.
First of all we will create a form and submit the form values after that we post this form value by using PHP.
Steps to Create a contact form with PHP
You have to follow, below provided steps carefully to create form by using PHP:
- First of all you have to open any one code editor (Here I am using VS code).
- Now you need to create a folder and name it as "contact_Form".
- Now you need to create a file "style.css" inside the project folder.
- Again create a new file "form.php" inside the project folder.
- After creating the files add the below codes in respective to codes as given below.
- Now you have successfully created contact form with PHP.
Adding CSS Code
You have to open style.css code inside the project folder.
Add the below provided CSS code inside style.css file.
Now you have to save the style.css file after adding the CSS code.
*,*:after,*:before{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
}
body{
font-family: arial;
font-size: 16px;
margin: 0;
background: #fff;
color: #000;
display: flex;
align-items:center;
justify-content: space-around;
min-height: 100vh;
}
.form-container{
width: 100%;
max-width: 600px;
margin:0 auto;
background: #09aad0;
padding: 20px;
border-radius: 10px;
color: #fff;
}
.input-row{
margin-bottom: 10px;
}
.input-row label{
display: block;
margin-bottom: 3px;
}
.input-row input,
.input-row textarea{
width: 100%;
padding: 10px;
border:0;
border-radius: 3px;
outline: 0;
margin-bottom: 3px;
font-size: 18px;
font-family: arial;
}
.input-row textarea{
height: 100px;
}
.input-row input[type="submit"] {
width: 100px;
margin:0 auto;
display: block;
text-align: center;
background: #002f3a;
color: #ffffff;
cursor: pointer;
}
.success {
background-color: #9fd2a1;
padding: 5px 10px;
color: #326b07;
text-align: center;
border-radius: 3px;
font-size: 14px;
margin-top: 10px;
}
Adding PHP Code
You have to open form.php code inside the project folder.
Add the below provided PHP code inside the form.php file.
Now you have to save the form.php file after adding the PHP code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=1">
<title>Form Submit to Send Email</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?php
if(!empty($_POST["send"])) {
$userName = $_POST["userName"];
$userEmail = $_POST["userEmail"];
$userPhone = $_POST["userPhone"];
$userMessage = $_POST["userMessage"];
$toEmail = "[email protected]";
$mailHeaders = "Name: " . $userName .
"\r\n Email: ". $userEmail .
"\r\n Phone: ". $userPhone .
"\r\n Message: " . $userMessage . "\r\n";
if(mail($toEmail, $userName, $mailHeaders)) {
$message = "Your contact information is received successfully.";
}
}
?>
<div class="form-container">
<form name="contactFormEmail" method="post">
<div class="input-row">
<label>Name <em>*</em></label>
<input type="text" name="userName" required id="userName">
</div>
<div class="input-row">
<label>Email <em>*</em></label>
<input type="email" name="userEmail" required id="userEmail">
</div>
<div class="input-row">
<label>Phone <em>*</em></label>
<input type="text" name="userPhone" required id="userPhone">
</div>
<div class="input-row">
<label>Message <em>*</em></label>
<!-- <textarea name="userMessage" required id="userMessage"> -->
</div>
<div class="input-row">
<input type="submit" name="send" value="Submit">
<?php if (! empty($message)) {?>
<div class='success'>
<strong><?php echo $message; ?> </strong>
</div>
<?php } ?>
</div>
</form>
</div>
</body>
</html>
After adding the PHP Code, make sure to add change the default details from the PHP code.
Now you can test the code in your local host and check messages are receiving or not and check if any issues or errors, if any errors detected, you try to solve them, else you can ask us through contact page.
Conclusion
I hope that you have understood about creating contact form with PHP code easily without any issue or errors.
If you ran into an issue, then ask us through contact page, we will respond to you as soon as possible.