Read Also - How to Edit Dynamic HTML Field by using Ajax with PHP
Hello Friends this tutorial is relating to dynamically insert or delete input html fields using Jquery Ajax with PHP MySql. User can insert more than one data at the same time. User can Add bulk data at same time. Data will be inserted with out page refresh because we use ajax function call for insert data. User can add more fields by clicking on add more button then new textbox will be appear on webpage with remove button. If user want to remove some field then it can be remove input fields on click on remove button. Now a days this functionality is very useful in web application.
See Also
Source Code
index.php
<html>
<head>
<title>Dynamically Add or Remove input fields in PHP with JQuery</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<h2 align="center">Dynamically Add or Remove input fields in PHP with 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">
<tr>
<td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>
<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></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");
$('#row'+button_id+'').remove();
});
$('#submit').click(function(){
$.ajax({
url:"name.php",
method:"POST",
data:$('#add_name').serialize(),
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
</script>
name.php
<?php
$connect = mysqli_connect("localhost", "root", "", "test_db");
$number = count($_POST["name"]);
if($number > 0)
{
for($i=0; $i<$number; $i++)
{
if(trim($_POST["name"][$i] != ''))
{
$sql = "INSERT INTO tbl_name(name) VALUES('".mysqli_real_escape_string($connect, $_POST["name"][$i])."')";
mysqli_query($connect, $sql);
}
}
echo "Data Inserted";
}
else
{
echo "Please Enter Name";
}
?>
Useful guide! IS there any official PHP tutorial website for learn the new version of cake PHP.
ReplyDeleteRegards,
PHP Training in Chennai|PHP Course in Chennai
hai teri chummia aise loon ga ke tu chennai bhool jaye gi ray
DeleteHello your demo is interesting but I have a problem how do I do if I want to add 3 fields and once add a message that redirects me to another page thank you in advance this urgent help me please
ReplyDeleteHi Abdoul Ali, I modified the code and I did what you want, please check it below:
DeleteSupper Amazing Article....Thanks
ReplyDeleteNice post and really cool info about jquery codings.
ReplyDeleteAndriod Training Courses in Chennai
excelent!
ReplyDeletebe projects in chennai
ReplyDelete2016 ieee projects in chennai
lenovo ideapad service center chennai
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletenice.
ReplyDeleteHadoop-Big-Data-Administration
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteNice post and really cool info about jquery codings.
ReplyDeletetib co training in chennai
Useful guide!
ReplyDeletemsbi training in chennai
This comment has been removed by a blog administrator.
ReplyDeleteExcellent Post.. Thank You Keep Updating...
ReplyDeleteEmbedded Training Institute in Chennai | Embedded Training with Placement in Chennai | Embedded Project Center in Chennai
how to add all submitted value into a single row name name1 name2 etc it is possible
ReplyDeleteplease give your contact no.
DeleteAbsolutely good!
ReplyDeletenice.
ReplyDeleteinteresting post
ReplyDeleteI am in love with your articles. God bless you
ReplyDeleteIts not saving data into database giving me this error?
ReplyDeleteNotice: Undefined index: name in C:\xampp\htdocs\addfield\name.php on line 3
Please Enter Name
Thanks So Much
ReplyDeleteThis tutorial is great! Thank you so much for sharing. Question: how would the script need to be altered if I want to enter more than just one field, for example: firstname, lastname, gender and age?
ReplyDeleteThank you!! its working
ReplyDeletehow can I add more column with existing database.....
ReplyDeleteYour code is great...thanks for sharing it
ReplyDeleteHow to redirect page after click ok (alert)?
ReplyDeleteHow to redirect page after click ok (alert)?
ReplyDeleteYour Database is not Working.
ReplyDeleteYour Database is not working.And I added 6 table but when I cut any one X all table has been remove.
ReplyDeleteThanks a bunch,you saved me
ReplyDeleteHi. So I used your method to dynamically add my input to the sql database, but I had a hard time updating it. Basically fetching from sql displayed as one input tag per row then will be able to update or delete. Ps. using a modal. Is it possible? TIA
ReplyDeleteI didnt undersrand this part plase help
ReplyDeletename="name[]"
hello, can u help me if i want to make the input into select button... and the select button will display data from database
ReplyDeleteVery informative post! Thanks for providing us this information. Keep us updated by sharing more such articles.
ReplyDeleteTally Course in Chennai
Tally Classes in Chennai
Tally Training in Chennai
Oracle Training in Chennai
Manual Testing Training in Chennai
WordPress Training in Chennai
JavaScript Training in Chennai
LINUX Training in Chennai
if I want to store numbers what to change in the code
ReplyDeletenot working for php pls help
ReplyDeleteI think that if you use increment with the add field button, then you should also use decrement with the delete field button. why do I need to add and remove buttons if they are not full.
ReplyDeleteAfter reading your blog, I was quite interested to learn more about this information. Thanks for sharing.
ReplyDeleteNo:1 Best PGDCA Training Center in kanchipuram|
Como fazer ele funcionar com o select2 nos campos adicionais?
ReplyDeletehey .its a amazing post.thanks for sharing a great article.keep it up
ReplyDeletewww.offsidetech.com
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command. Best MS Office Training Institute In Kanchipuram|
ReplyDeleteYou have made some good points there. I looked on the net for additional information about the issue and found most individuals will go along with your views on this site.
ReplyDeleteTech geek
Very nice blogs!!! I have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.…
ReplyDeleteMatlab Project Center in Chennai |
Matlab Project Center in Chennai
I would like to get source code of this webslesson.info/2018/03/ajax-crud-on-dynamically-add-remove-input-feilds-in link. Plz advice me or send to my mail.
ReplyDeleteNice Work!
ReplyDeleteRegards,
Solution Views
Awesome post! Greetings from Bimal's Blog
ReplyDeletethanks.....thats great
ReplyDeleteThank u for sharing such nice information.!! Total recall is also good one.!! PMP Ceritification in Chennai|
ReplyDeleteNice Blog...looking amazing...thank you so much for sharing such a wonderful blog...keep sharing... Best CCNA Certification In Chennai|
ReplyDeletethis blog is really useful and it is very interesting thanks for sharing , it is really good and exclusive. Best Prince 2 Exam center in chennai|
ReplyDeleteYou are the best. You saved me 2 working days. Thanks!
ReplyDeletemerci beaucoup pour ce tutoriel
ReplyDeleteVery happy to see this blog. Gives a wonderful information with coded explanaion. Thank you for this blog. very useful to me. Prince2 Certification Center in Chennai |
ReplyDeleteGreat article Glad to find your blog. Thanks for sharing.
ReplyDeletePCB Design Training Institute in Chennai | PCB Training Center in Velachery | PCB Design Course in Velachery
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteGreat Tutorial.
ReplyDeleteCan results from MYSQL while loop be used for new added lines
How to change input field to selection option that can be add and remove
ReplyDeletehow to change from input text to selection that value get from database and can add and remove
ReplyDelete“Great share!” Each and every tips of your post are more informative and it was awesome to read.
ReplyDeleteThank you for such a great post…..
if someone click on add button and fill only the first form still it is getting submitted to database after clicking the submit button how can i make it mandatory to fill the extra form too
ReplyDeletethank you so much
ReplyDeleteNice blog. I feel really happy to have seen your web page and look forward to so many more amusing times reading here. Thanks once more for all the details.
ReplyDeletePythonFullStack training in Chennai
PythonFullStack training in Velachery
PythonFullStack training in Thambaram
PythonFullStack training in Guindy
PythonFullStack training in Tharamani