Submit PHP Form without Page Refresh

Submit PHP Form without Page Refresh

In this article, we are going to create on how to submit Submit PHP Form without Page Refresh using jQuery and Ajax. We are going to use these two functions so we can submit PHP Forms without any refreshing in our page and we are going to use the $_POST request in PHP. Let's do this:

Creating Simple HTML Form

Kindly copy and paste to your BODY tag of your page. Save it as "index.php".
  1. <div id="container">
  2. <div id="form" class="result_1">
  3. <form method="post" id="registration_Form">
  4. <table border="1">
  5. <tr>
  6. <td><label>First Name</label></td>
  7. <td><input type="text" name="first_name" id="first_name" placeholder="Enter your First Name" autofocus="autofocus"/></td>
  8. </tr>
  9. <tr>
  10. <td><label>Last Name</label></td>
  11. <td><input type="text" name="last_name" id="last_name" placeholder="Enter your Last Name" /></td>
  12. </tr>
  13. <tr>
  14. <td><label>User Name</label></td>
  15. <td><input type="text" name="user_name" id="user_name" placeholder="Enter your User Name" /></td>
  16. </tr>
  17. <tr>
  18. <td><label>Password</label></td>
  19. <td><input type="text" name="password" id="password" placeholder="Enter your Password" /></td>
  20. </tr>
  21. <tr>
  22. <td><label>Email</label></td>
  23. <td><input type="text" name="email" id="email" placeholder="Enter your Email" /></td>
  24. </tr>
  25. <tr>
  26. <td colspan="2"><button type="submit">Register</button></td>
  27. </tr>
  28. </table>
  29. </form>
  30. </div>
  31. </div>

Script

Copy and paste to your HEAD tag of your page in index.php.
  1. <script type="text/javascript" src="js_code.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function()
  4. {
  5. $(document).on('submit', '#registration_Form', function()
  6. {
  7. var data = $(this).serialize();
  8. $.ajax({
  9. type : 'POST',
  10. url : 'submit.php',
  11. data : data,
  12. success : function(data)
  13. {
  14. $("#registration_Form").fadeOut(500).hide(function()
  15. {
  16. $(".result_1").fadeIn(500).show(function()
  17. {
  18. $(".result_1").html(data);
  19. });
  20. });
  21.  
  22. }
  23. });
  24. return false;
  25. });
  26. });
  27. </script>

Result Page

Kindly copy and paste this source code and save it as "submit.php".
  1. <?php
  2.  
  3. if($_POST)
  4. {
  5. $first_name = $_POST['first_name'];
  6. $last_name = $_POST['last_name'];
  7. $user_name = $_POST['user_name'];
  8. $password = $_POST['password'];
  9. $email = $_POST['email'];
  10. ?>
  11. <table border="0">
  12. <tr>
  13. <td colspan="2" style="color:blue; font-size:30px; font-family:cursive; font-weight:bold;">Successfully Registered !!!</td>
  14. </tr>
  15. <tr>
  16. <td colspan="2"><hr /></td>
  17. </tr>
  18. <tr>
  19. <td style="color:blue; font-size:18px; font-family:cursive; font-weight:bold;">First Name</td>
  20. <td style="color:red; font-size:18px; font-family:cursive; font-weight:bold;"><?php echo $first_name; ?></td>
  21. </tr>
  22. <tr>
  23. <td style="color:blue; font-size:18px; font-family:cursive; font-weight:bold;">Last Name</td>
  24. <td style="color:red; font-size:18px; font-family:cursive; font-weight:bold;"><?php echo $last_name; ?></td>
  25. </tr>
  26. <tr>
  27. <td style="color:blue; font-size:18px; font-family:cursive; font-weight:bold;">User Name</td>
  28. <td style="color:red; font-size:18px; font-family:cursive; font-weight:bold;"><?php echo $user_name; ?></td>
  29. </tr>
  30. <tr>
  31. <td style="color:blue; font-size:18px; font-family:cursive; font-weight:bold;">User Name</td>
  32. <td style="color:red; font-size:18px; font-family:cursive; font-weight:bold;"><?php echo $password; ?></td>
  33. </tr>
  34. <tr>
  35. <td style="color:blue; font-size:18px; font-family:cursive; font-weight:bold;">Email</td>
  36. <td style="color:red; font-size:18px; font-family:cursive; font-weight:bold;"><?php echo $email; ?></td>
  37. </tr>
  38. </table>
  39. <?php
  40. }
  41. ?>

Result:

ResultResult That's all, you can submit PHP Forms without page refreshing. 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