PHP - Submit Form Data Using Ajax POST Method

In this tutorial we will create Submit Form Data Using Ajax POST Method. This code can compressed multiple files at the same time. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_submit, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_submit');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Submit Form Data Using Ajax post Method</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <form>
  20. <div class="form-group">
  21. <label>Firstname:</label>
  22. <input type="text" id="firstname" class="form-control"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Lastname:</label>
  26. <input type="text" id="lastname" class="form-control"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Address:</label>
  30. <input type="text" id="address" class="form-control"/>
  31. </div>
  32. <br />
  33. <div class="form-group">
  34. <button type="button" id="submit" class="btn btn-primary form-control">Submit</button>
  35. </div>
  36. </form>
  37. </div>
  38. </div>
  39. </body>
  40. <script src="js/jquery-3.2.1.min.js"></script>
  41. <script src="js/bootstrap.js"></script>
  42. <script src="js/script.js"></script>
  43. </hmtl>

Creating PHP Query

This code contains the php query of the application. This code will sent the data inputs to the database server by sending ajax post request method. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $firstname = $_POST['firstname'];
  5. $lastname = $_POST['lastname'];
  6. $address = $_POST['address'];
  7.  
  8. $conn->query("INSERT INTO `student` VALUES('', '$firstname', '$lastname', '$address')");
  9.  
  10. echo "Data Inserted!";
  11. ?>

Creating Ajax Post Script

This is where the code that uses ajax request been used. This code will sent a post request to process the data and successfully to be send to the database. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2. $('#submit').on('click', function(){
  3. var firstname = $('#firstname').val();
  4. var lastname = $('#lastname').val();
  5. var address = $('#address').val();
  6.  
  7. if(firstname == '' || lastname == '' || address == ''){
  8. alert("Please complete the required field!");
  9. }else{
  10. $.post('save.php', {firstname: firstname, lastname: lastname, address: address}, function(data){
  11. alert(data);
  12. $('#firstname').val('');
  13. $('#lastname').val('');
  14. $('#address').val('');
  15. });
  16. }
  17. })
  18. });
There you have it we successfully created Submit Form Data Using Ajax POST Method. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment