PHP - Dynamically Add Remove input fields using JQuery Ajax Example with Demo

By Hardik Savani | December 3, 2017 | | 29860 Viewer| Category : PHPBootstrapjQueryMySqlAjax

In this post, we will learn how to add and remove form input fields dynamically using jQuery and store in database using PHP. Here you will see how to handle dynamically added fields value save in mysql database using PHP Bootstrap. I will show you full example of dynamically add/remove input fields and submit to database with jquery ajax and php. you can also see add more fields jquery demo.

Few days ago i posted add/remove multiple input fields dynamically with jquery ajax in Laravel Framework, You can see here : Laravel - Dynamically Add or Remove input fields using JQuery.

We almost need to require add more functionality when client want to at time multiple value insert into database. It will great if you are use something interesting like give them input box with "+" button that way they can add multiple values at time.

So here you have to just follow few step to proceed.

1) Create Database Table

2) index.php File

3) addmore.php File

After Completed full example, you will get layout like as bellow:


Step 1: Create Database Table

In fist step, we need to create database and table, so here i created "test" database and "tagslist" table with id and name column. You can simply create "tagslist" table as following sql query.

SQL Query:


`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,


) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=24 ;

Step 2: Create index.php File

Here, we need to create index.php file and i created form with one input text box and button. I also write code for add more fields in jquery. So let's create index.php file and put bellow code.


<!DOCTYPE html>



<title>PHP - Dynamically Add or Remove input fields using JQuery</title>

<link rel="stylesheet" href="" />

<script src="//"></script>

<script src=""></script>



<div class="container">

<h2 align="center">PHP - Dynamically Add or Remove input fields using JQuery</h2>

<div class="form-group">

<form name="add_name" id="add_name">

<div class="table-responsive">

<table class="table table-bordered" id="dynamic_field">


<td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" required="" /></td>

<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>



<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />





<script type="text/javascript">


var postURL = "/addmore.php";

var i=1;



$('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" required /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');


$(document).on('click', '.btn_remove', function(){

var button_id = $(this).attr("id");














alert('Record Inserted Successfully.');








Step 3: Create addmore.php File

In this step, we will write code of insert data into database using mysql query. So you have to create addmore.php and put bellow code:



define (DB_USER, "root");

define (DB_PASSWORD, "root");

define (DB_DATABASE, "test");

define (DB_HOST, "localhost");

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);


foreach ($_POST["name"] as $key => $value) {

$sql = "INSERT INTO tagslist(name) VALUES ('".$value."')";



echo json_encode(['success'=>'Names Inserted successfully.']);



Now you are ready to run example.

I hope it can help you...

We are Recommending you: