The Contact Form is an essential element for almost every website. The contact us form allows visitors to communicate with the site owner from the website. Using the contact us form, visitors can easily submit their queries, views, opinions, and suggestions to the site administrator about the website, service, or product. Also, the submitted information can be sent to the site owner or administrator via email.
Contact form helps you to receive the query from visitors and provide a quick response to the visitors. The thought of a contact form is very simple, the user is able to send their query via email to the respective organization. In this tutorial, we’ll show you how to create a simple contact form with PHP and integrate it into your website. Using our PHP contact form script you’ll be able to add a contact us form to your website within 5 minutes. The contact form not only is submitted but also an email will be sent to you on every form submission using PHP.
For better understanding, we are going to divide the PHP contact form script into two parts, HTML and PHP. You can place this code together on the web page where you want to display the contact us form.
The following HTML will display a contact form with some common fields (Name, Email, Subject, and Message) and a submit button. Add this entire code to the web page to display the contact us form.
<!-- Status message -->
<?php if(!empty($statusMsg)){ ?>
<div class="status-msg <?php echo $status; ?>"><?php echo $statusMsg; ?></div>
<?php } ?>
<!-- Form fields -->
<form action="" method="post">
<div class="form-input">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Enter your name" value="<?php echo !empty($postData['name'])?$postData['name']:''; ?>" required="">
</div>
<div class="form-input">
<label for="email">Email</label>
<input type="email" name="email" placeholder="Enter your email" value="<?php echo !empty($postData['email'])?$postData['email']:''; ?>" required="">
</div>
<div class="form-input">
<label for="subject">Subject</label>
<input type="text" name="subject" placeholder="Enter subject" value="<?php echo !empty($postData['subject'])?$postData['subject']:''; ?>" required="">
</div>
<div class="form-input">
<label for="message">Message</label>
<textarea name="message" placeholder="Type your message here" required=""><?php echo !empty($postData['message'])?$postData['message']:''; ?></textarea>
</div>
<input type="submit" name="submit" class="btn" value="Submit">
</form>
After the submission, the form data is posted to a server-side script (submit.php
) for processing. Include the server-side form submission script at the top of the file where the contact form HTML is placed.
<?php
// Form submission script
include_once 'submit.php';
?>
This submit.php
is a PHP script that handles the form submission process and email sending functionality.
FILTER_VALIDATE_EMAIL
filter.<?php
// Email configuration
$toEmail = 'admin@example.com';
$fromName = 'Sender Name';
$formEmail = 'sender@example.com';
$postData = $statusMsg = $valErr = '';
$status = 'error';
// If the form is submitted
if(isset($_POST['submit'])){
// Get the submitted form data
$postData = $_POST;
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
// Validate form fields
if(empty($name)){
$valErr .= 'Please enter your name.<br/>';
}
if(empty($email) || filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$valErr .= 'Please enter a valid email.<br/>';
}
if(empty($subject)){
$valErr .= 'Please enter subject.<br/>';
}
if(empty($message)){
$valErr .= 'Please enter your message.<br/>';
}
if(empty($valErr)){
// Send email notification to the site admin
$subject = 'New contact request submitted';
$htmlContent = "
<h2>Contact Request Details</h2>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Subject: </b>".$subject."</p>
<p><b>Message: </b>".$message."</p>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Header for sender info
$headers .= 'From:'.$fromName.' <'.$formEmail.'>' . "\r\n";
// Send email
@mail($toEmail, $subject, $htmlContent, $headers);
$status = 'success';
$statusMsg = 'Thank you! Your contact request has submitted successfully, we will get back to you soon.';
$postData = '';
}else{
$statusMsg = '<p>Please fill all the mandatory fields:</p>'.trim($valErr, '<br/>');
}
}
PHP Contact Form with Google reCAPTCHA
You can build a simple contact form with email functionality using this example script. This contact form script can be enhanced easily as per your needs. If you want to protect the contact form against spam, add CAPTCHA functionality.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
How to prevent reloading of the page after submission ?
Hi!
It works perfectly but I need to add a select with options to the form, would it affect anyhow? can you give me an example of what’ll change if I add it?
thanks!
Thank you for very informative tutorial you shared here. Please do I need to create database on hosting server before this code can run? Thanks
Very informative tutorial. I love it. Please I am a new PHP learner, before all this code should work do I need to create database with table on the hosting server ? Please I would appreciate your response as regards this.
Hi, how do i respond to the contact message afterwards. ? Thank you in advance.
Hello
Nice article thanks for all the
Hello thanks for the tutorial , But am geting this waning after testing the downloaded code.
Warning: mail(): Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in C:\xampp\htdocs\simple\index.php on line 40
How can i go about it sir?
PHP mail function will not work on localhost. You need to use SMTP server to send email from localhost. This tutorial may help you – https://www.codexworld.com/how-to-send-email-from-localhost-in-php/
one question how can i clean the echo message after sending the message? its always appearing after i sent a message i dont know how i clean after refreshing.
Define
$statusMsg = ''
to prevent the status message after form submit.do you have tutorials that can use a yahoo mail? i mean users can also use a yahoo mail to send their message?
We have used “your_email@gmail.com” for example purpose, you can use Yahoo or any other email address.
I have my form set up at the bottom of my index page before the footer. I copied all of your code correctly. However…
When I click on the send button, the page simply jumps at the beginning of the index page. it seems like nothing happens. Could you guess why is that?
Make sure that you have placed the PHP form submission script at the beginning of the page.
do I need the phpmailer file? or just need to copy this code?
You don’t need phpmailer or any other file. Just use our script given in the tutorial.