Introduction
 
In this article I show how to create a "Contact Us" form in PHP. This form is very simple and anyone can use this to help understand PHP. You can edit and add fields. You will first create a HTML file for the Contact Us form design. In the form field you will set the action for your PHP file.
 
Example 
 
This is your HTML file:
  1. <html>  
  2. <head>  
  3. <title>Contact Form</title>  
  4. </head>  
  5. <body bgcolor="#CED2B9">  
  6. <table width="400" border="0" align="center" cellpadding="3" cellspacing="1">  
  7. <tr>  
  8. <td><strong>Contact Form</strong></td>  
  9. </tr>  
  10. </table>  
  11. <table width="400" border="0" align="center" cellpadding="0" cellspacing="1">  
  12. <tr>  
  13. <td>  
  14. <form name="form1" method="post" action="send.php">  
  15. <table width="100%" border="0" cellspacing="1" cellpadding="3">  
  16. <tr>  
  17. <td>Email</td>  
  18. <td>:</td>  
  19. <td>  
  20. <input name="email" type="text" id="customer_mail" size="20">  
  21. </td>  
  22. </tr>  
  23. <tr>  
  24. <td>Name</td>  
  25. <td>:</td>  
  26. <td>  
  27. <input name="name" type="text" id="name" size="20">  
  28. </td>  
  29. </tr>  
  30. <tr>  
  31. <td width="16%">Subject</td>  
  32. <td width="2%">:</td>  
  33. <td width="82%">  
  34. <input name="sub" type="text" id="subject" size="24">  
  35. </td>  
  36. </tr>  
  37. <tr>  
  38. <td>Detail</td>  
  39. <td>:</td>  
  40. <td>  
  41. <textarea name="msg" cols="20" rows="4" id="detail"></textarea>  
  42. </td>  
  43. </tr>  
  44. <tr>  
  45. <td> </td>  
  46. <td> </td>  
  47. <td>  
  48. <input type="submit" name="Submit" value="Submit">  
  49. <input type="reset" name="Submit2" value="Reset">  
  50. </td>  
  51. </tr>  
  52. </table>  
  53. </form>  
  54. </td>  
  55. </tr>  
  56. </table>  
  57. </body>  
  58. </html>  
In the PHP file I use the mail() function to send the contact information but this function does not work for the local host. You can see that I have described below how to sendi the contact. I will create a PHP file for data submission.
  1. <?php  
  2. $subject = $_POST['sub'];  
  3. $name = $_POST['name'];  
  4. $from = $_POST['email'];  
  5. $detail = $_POST['msg'];  
  6. $subject ="$subject";  
  7. $message="$detail";  
  8. $mail_from="$from";  
  9. $header="from: $name <$mail_from>";  
  10. $to ='[email protected]';  
  11. //form validation with PHP code  
  12. if($name != "" && $from != "" && $subject != "" && $message != "")  
  13. {  
  14. mail($to,$subject,$message,$header);  
  15. }  
  16. else   
  17. {   
  18. echo "Please fill in all fields and submit again!";   
  19. }  
  20. ?>  
image 1.jpg
 
When you submit a blank field, this error is generated:
 
image 3.jpg
image2.jpg
 

Next Recommended Readings