How to create custom html contact form in php

How to create custom html contact form in php
In this tutorial we will learn how to create a custom html contact form in php. Contact form is very important for websites and used for the user feedback. Contact forms basically consist of three basic fields Name, email and message. First we will create a html file called form.php consist of these fields and a submit button the submit button will be used to send the form data enter by the user to specific email which we will define.
 <form action="contact.php" method="post">  
 <label>Name:</label>  
     <input name="Name" placeholder="Name:" type="text" required/>  
                 <label>Email:</label>  
     <input name="Email" placeholder="Email:" type="text" required />   
                 <label>Message:</label>  
                 <textarea name="Message" placeholder="Message:" rows="10" required></textarea>  
                 <input name="submit" type="submit" value="Submit" />  
               </form>  

Php code to submit contact form

The 2nd step is to create a php file name it contact.php which will consist of php code to send email when the form is submitted. Define an email to which the form is send and define a subject variable and write a appropriate subject for the email. All the data of the form is submitted through POST Method and save to a variable and used a php mail function to send email. Now we will check through IF statement that the form is submitted and will show a message “Your Form is submitted successfully” and if form is fail to submit will show “Error occurred”.
In this article you learn that how to create a custom contact form in php. Here is a quick review first create a form.php file and put all the necessary fields and in the 2nd step write a php code to send email.
 if (isset($_POST["submit"]))   
  {   
   $name = $_POST["Name"];   
   $email = $_POST["Email"];  
  $message = $_POST["Message"];  
   $to='xyz@gmail.com';   
  $subject='Feedback';   
   $body=$name."\n".$message;   
   $header='From: '.$email;   
   // send mail  
   $sent=mail($to ,$email,$message,"From: $name\n");   
   if($sent){   
     echo '<span class="error">Email Sending Failed. Please try again later.</span>';   
   }else{   
   echo '<span class="alert-success">Thank you for sending us feedback</span>';   
   }  
   }  
Share on Google Plus

About zeb

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment