PHP - Simple Dynamic Inputs Using jQuery

In this tutorial we will create a Simple Dynamic Inputs Using jQuery. By 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. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. 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_input 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_input");
  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>
  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 - Simple Dynamic Inputs Using jQuery</h3>
  16.                 <hr style="border-top:1px dotted;"/>
  17.                 <div class="col-md-3"></div>   
  18.                 <div class="col-md-6">
  19.                         <form method="POST" action="save.php">
  20.                                 <div class="form-group">
  21.                                         <input type="text" id="input" class="form-control"/>
  22.                                 </div>
  23.                                 <center><button type="button" id="add" class="btn btn-primary">Add Input</button></center>
  24.                                 <br /> 
  25.                                 <div id="input_container"></div>
  26.                                 <center><button style="display:none;" name="submit" id="submit" class="btn btn-success">Submit</button></center>
  27.                         </form>
  28.                 </div> 
  29.         </div>
  30. <script src="js/jquery-3.2.1.min.js"></script>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating PHP Query

This code contains the php query of the application. This code will save the data inputs to the MySQL database server. 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.         if(ISSET($_POST['submit'])){
  5.                         foreach($_POST['newInput'] as $key => $value){
  6.                                 mysqli_query($conn, "INSERT INTO `input` VALUES('', '$value')") or die(mysqli_error());
  7.                         }
  8.                         header('location: index.php');
  9.         }
  10. ?>

Creating The Script

This is where the main function of the application. This code will create a new input text every time the user click the add button. To do this just copy and write these block of codes inside the text editor, then save it inside the js directory as script.js.
  1. $(document).ready(function(){
  2.         $('#add').on('click',function(){
  3.                 var input = $('#input').val();
  4.                 if(input != ""){
  5.                         $('#submit').show();
  6.                         var newInput = $(
  7.                         '<div class="form-group">'
  8.                         +'<input type="text" name="newInput[]" placeholder="'+input+'" class="form-control" required="required"/>'
  9.                         +'</div>'
  10.                         );
  11.                         newInput.appendTo('#input_container');
  12.                         $('#input').val('');
  13.                 }else{
  14.                         alert("Please enter something");
  15.                 }
  16.         });
  17. });
There you have it we successfully created Simple Dynamic Inputs 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