Create contact form with php

Date posted : 13/02/2019

In this post we will explain how to create the contact form with php. Customer will contact the website owner using this contact form.

Step 1: Create the contact form using the following code.

<form name="contactform" method="post" action="send_form_email.php">
<table width="1000px">
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>

<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit"> 
 </td>
</tr>
</table>
</form>

Step 2: Create the send_form_email and paste the following code given below.

Note: You can modify the email to your email address. The following code execute in server then mails will work fine.

<?php
$email_to ="example@email.com";
$email_subject = "Enquiry form";
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);  
?>
Thank you for contacting us
<?php
?>

That’s it. Contact form developed using Php 🙂

Thanks for using pheonix solutions.

You find this tutorial helpful? Share with your friends to keep it alive.