Creating a Simple Contact Form with PHP

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.

Contact Form HTML

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';
?>

Contact Form Submission Script (submit.php)

This submit.php is a PHP script that handles the form submission process and email sending functionality.

  • Use PHP $_POST variable to retrieve the value of form fields.
  • Validate email address using PHP FILTER_VALIDATE_EMAIL filter.
  • Send an email with form data using the PHP mail() function.
<?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($emailFILTER_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

Conclusion

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

16 Comments

  1. Krat Ohm Said...
  2. Vgo Said...
  3. Abiodun Ilesanmi Said...
  4. Abiodun Ilesanmi Said...
  5. Nick Said...
  6. Purushotam Said...
  7. Lyada Emmanuel Said...
  8. Lin Said...
    • CodexWorld Said...
  9. Lin Said...
    • CodexWorld Said...
  10. Željka Said...
    • CodexWorld Said...
  11. Nuffsaid Said...
    • CodexWorld Said...

Leave a reply

keyboard_double_arrow_up