How To Create Contact Form Using PHP

Related Code: User Registration Form Using PHP In this tutorial, we are going to learn on How To Create Contact Form Using PHP. We have a contact form in the example below for the user to send their message, suggestions, questions, feedback, and clarifications about your work or your website. We have three fields as full name, email, and message in our contact form. This is a very simple tutorial, hope you will find this useful. Contact Form - HTML This source code for our contact form.
  1. <form name="frmContact" method="post" action="">
  2.  
  3. <div class="aler_message"><?php if(isset($message)) { echo $message; } ?></div>
  4.  
  5. <table border="0" cellpadding="10" cellspacing="1" width="500" align="center">
  6. <tr class="tableheader">
  7. <td colspan="2">Contact Form</td>
  8. </tr>
  9. <tr class="tablerow">
  10. <td>Full Name<br/> <input type="text" class="text_input" autofocus="autofocus" name="fullname"></td>
  11. <td>Email<br/> <input type="text" class="text_input" autofocus="autofocus" name="email"></td>
  12. </tr>
  13. <tr class="tablerow">
  14. <td colspan="2">Message<br/><textarea name="user_message" class="text_input" autofocus="autofocus" cols="60" rows="6"></textarea></td>
  15. </tr>
  16. <tr class="tableheader">
  17. <td colspan="2"><input type="submit" class="btn_submit" name="submit" value="Submit"></td>
  18. </tr>
  19.  
  20. </form>
Data Table This is the data where the user message shows in this table.
  1. <table border="1" class="table_data" cellspacing="5" cellpadding="5">
  2. <tr style="color:blue;">
  3.  
  4. <th>
  5. Full Name
  6. </th>
  7. <th>
  8. Email
  9. </th>
  10. <th>
  11. Message
  12. </th>
  13. </tr>
  14. <?php
  15. $conn = mysql_connect("localhost","root","");
  16. mysql_select_db("contact_form",$conn);
  17. $result= mysql_query("select * from tbl_contact order by tbl_contact_id DESC ") or die (mysql_error());
  18. while ($row= mysql_fetch_array ($result) ){
  19. $id=$row['tbl_contact_id'];
  20. ?>
  21. <tr style="text-align:center; color:blue;">
  22. <td style="width:200px;">
  23. <?php echo $row['fullname']; ?>
  24. </td>
  25. <td style="width:200px; color:red;">
  26. <?php echo $row['email']; ?>
  27. </td>
  28. <td style="width:200px; color:blue;">
  29. <?php echo $row['user_message']; ?>
  30. </td>
  31. </tr>
  32. <?php } ?>
  33. </table>
And, we have CSS style.
  1. table {
  2. border:yellow 5px solid;
  3. }
  4. .table_data {
  5. margin-left: 291px;
  6. margin-top: 20px;
  7. }
  8. .tableheader {
  9. background-color: maroon;
  10. color: white;
  11. font-weight: bold;
  12. font-size: 25px;
  13. }
  14. .tablerow {
  15. background-color: black;
  16. color: white;
  17. font-size: 20px;
  18. font-weight: bold;
  19. }
  20. .aler_message {
  21. color: #FF0000;
  22. font-weight: bold;
  23. text-align: center;
  24. width: 100%;
  25. cursor:pointer;
  26. }
  27. .text_input {
  28. font-size: 18px;
  29. border: blue 1px solid;
  30. background: azure;
  31. text-indent: 10px;
  32. cursor:pointer;
  33. }
  34. .btn_submit {
  35. font-size: 18px;
  36. width: 100px;
  37. background: azure;
  38. border: blue 1px solid;
  39. padding: 6px;
  40. border-radius: 8px;
  41. color: blue;
  42. cursor:pointer;
  43. }
  44. .btn_submit:hover {
  45. font-size: 18px;
  46. width: 100px;
  47. background: azure;
  48. border: re 1px solid;
  49. padding: 6px;
  50. border-radius: 8px;
  51. color: red;
  52. cursor:pointer;
  53. }
Contact Form Input Into Database This is PHP query where our data store in the database. First, create a database table, name into "contact_form".
  1. $conn = mysql_connect("localhost","root","");
  2. mysql_select_db("contact_form",$conn);
  3. mysql_query("INSERT INTO tbl_contact (fullname, email, user_message) VALUES ('" . $fullname. "', '" . $email. "','" . $user_message. "')");
After executing the query code above it will be added to the database, this is the result. Add DataShow Data MessageRelated Code: User Registration Form Using PHP Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment