How to create simple contact Form in PHP and HTML

As we discussed and learned how to send email using email() in PHP in the previous article. Now in this article we will see creating simple contact form in PHP and HTML.

First of all we are going to create simple contact form in HTML which allows viewers to enter their information like “Full Name”, ”Subject”, ”E-mail”, “Message” these are common values that we want in our contact form. And then we will create PHP form to process these and send the mail. So first create new HTML file and name it as “index.html”.

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Simple Contact Us Form</title>

<meta http-equiv="Content- Type" content="text/html; charset=UTF-8" />

<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>

<div id="page-wrap">

<h1 align ="center">Simple <strong>Contact Us</strong> Form</h1>

<div id="form-area">

<form method="post" action="contactengine.php">

<label for="Name">Name:</label>

<input type="text" name="Name" id="Name" required/>

<label for="number">Contact Number:</label>

<input type="text" name="number" id="number"

required/>

<label for="Email">Email:</label>

<input type="text" name="Email" id="Email" required/>

<label for="Enquiry">Message:</label><br />

<textarea name="message" rows="20" cols="20" id="message"></textarea>

<input type="submit" name="submit" value="Submit" class="submit-button" />

</form>

</div>

</div>

</body>

</html>

 

Now next step is to create PHP form to send these values to the given mail. For that create another PHP file and name it as “contactengine.php” and follow the below code.

<?php

$EmailFrom = Trim(stripslashes($_POST['Email'])); //"lotsofthing13@gmail.com";

$EmailTo = "lotsofthing13@gmail.com";

$Subject = "You have a new email from contact form";

$Name = Trim(stripslashes($_POST['Name']));

$number = Trim(stripslashes($_POST['number']));

$Email = Trim(stripslashes($_POST['Email']));

$message = Trim(stripslashes($_POST['message']));

$headers = "From: $EmailFrom \r\n";

$headers .= "Cc: $Emailcc \r\n";

// validation

$validationOK=true;

if (!$validationOK) {

print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

exit;

}

// prepare email body text

$Body = "";

$Body .= "Name: ";

$Body .= $Name;

$Body .= "\n";

$Body .= "Contact Number: ";

$Body .= $number;

$Body .= "\n";

$Body .= "Email: ";

$Body .= $Email;

$Body .= "\n";

$Body .= "message: ";

$Body .= $message;

$Body .= "\n";

// send email

$success = mail($EmailTo, $Subject, $Body, $headers);

// redirect to success page

if ($success){

print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";

}

else{

print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

}

?>

This is how we can create a simple contact form using HTML and PHP for contact us for Enquire us as well. In the next article we extend this article for adding the header like ”CC” and “BCC” which is one of the wanted requirement of clients.

Download:  create-Simple-contact-form-with-different-field

Leave a Reply