PHP - Dynamic Switching Forms Using jQuery

In this tutorial we will create a Dynamic Switching Forms using jQuery. Ajax is a client-side script that communicates to a server/database without the need for a page refresh. It is mostly use by a well known websites like facebook. AJAX is a new technique for creating better, faster, and more interactive web applications. So let's do the coding...

Getting 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_switch, 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 = mysqli_connect("localhost", "root", "", "db_switch");
  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 you 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 - Dynamic Switching Forms Using jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-primary" id="add"><span class="glyphicon glyphicon-plus"></span> Add student</button>
  18. <br /><br />
  19. <div id="display"></div>
  20. </div>
  21. <script src="js/jquery-3.2.1.min.js"></script>
  22. <script src="js/script.js"></script>
  23. </body>
  24. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data to the database server using ajax request. 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. $name = $_POST['name'];
  5. $gender = $_POST['gender'];
  6. $age = $_POST['age'];
  7.  
  8. mysqli_query($conn, "INSERT INTO `student` VALUES('', '$name', '$gender', '$age')") or die(mysqli_error());
  9.  
  10. echo "Success saved!";
  11. ?>

Creating Main Function

This code contains the main function of the application. This code will dynamically change between two forms without refreshing the page. To do that just copy and write this block of codes inside the text editor, then save it inside as shown below. data.php
  1. <table class="table table-bordered">
  2. <thead class="alert-info">
  3. <tr>
  4. <th>Name</th>
  5. <th>Gender</th>
  6. <th>Age</th>
  7. </tr>
  8. </thead>
  9. <tbody style="background-color:#fff;">
  10. <?php
  11. require 'conn.php';
  12.  
  13. $query = mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  14. while($fetch = mysqli_fetch_array($query)){
  15. ?>
  16. <tr>
  17. <td><?php echo $fetch['name']?></td>
  18. <td><?php echo $fetch['gender']?></td>
  19. <td><?php echo $fetch['age']?></td>
  20. </tr>
  21. <?php
  22. }
  23. ?>
  24. </tbody>
  25. </table>
form.php
  1. <div class="col-md-3"></div>
  2. <div class="col-md-6" style="border:1px SOLID #ccc; padding:20px;">
  3. <form method="POST">
  4. <div class="form-group">
  5. <label>Name</label>
  6. <input type="text" id="name" class="form-control"/>
  7. </div>
  8. <div class="form-group">
  9. <label>Gender</label>
  10. <select id="gender" class="form-control">
  11. <option value="">Select an option</option>
  12. <option value="Male">Male</option>
  13. <option value="Female">Female</option>
  14. </select>
  15. </div>
  16. <div class="form-group">
  17. <label>Age</label>
  18. <input type="number" id="age" min="0" class="form-control"/>
  19. </div>
  20. <br />
  21. <div id="result"></div>
  22. <br />
  23. <center><button type="button" id="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button> <button type="button" id="view" class="btn btn-info"><span class="glyphicon glyphicon-eye-open"></span> View Data</button></center>
  24. </form>
  25. </div>
script.js Note: Make sure to save this file inside the js directory to make this script works
  1. $(document).ready(function(){
  2. displayData();
  3.  
  4. $('#add').on('click', function(){
  5. $(this).hide();
  6. displayForm();
  7. });
  8.  
  9.  
  10. $(document).on('click', '#view', function(){
  11. $('#add').show();
  12. displayData();
  13. })
  14.  
  15. $(document).on('click', '#save', function(){
  16. if($('#name').val() == "" || $('#gender').val() == "" || $('#age').val() == ""){
  17. $("#result").html("<center class='alert-danger'>Please complete the required field!</center>");
  18. }else{
  19. var name = $('#name').val();
  20. var gender = $('#gender').val();
  21. var age = $('#age').val();
  22.  
  23. $.post("save.php", {name: name, gender: gender, age: age}, function(data){
  24. $('#name').val('');
  25. $('#gender').val('');
  26. $('#age').val('');
  27.  
  28. $('#result').html("<center class='alert-success'>"+data+"</center>");
  29. });
  30. }
  31. })
  32.  
  33. function displayData(){
  34. $('#display').load("data.php");
  35. }
  36.  
  37. function displayForm(){
  38. $('#display').load("form.php");
  39. }
  40. });
  41.  
There you have it we successfully created Dynamic Switching Forms using jQuery. 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