You may find a need to allow the user to dynamically add or remove text inputs from your application. This is relatively easy to do using jQuery and using arrays to submit the form data to a PHP page. This tutorial runs through setting up the form with the controls to add, remove and reset the form fields as well as preparing the form variables to be sent using AJAX.
You won’t need to be a JavaScript master to work out what’s going on here. the first click functions control the number of input fields displayed on the page. Each new input field that’s added is given the ‘dynamic[]‘ array name so the values are concatenated and separated with a comma.
Then once the user submits the form, the form values are pushed into an array which can then be submitted using PHP to your database. You’ll probably need to either store the complete string for example 1,2,3,4,5,6,7 in the database or explode the string using PHP where you can manipulate it further.
$(document).ready(function(){
var i = $('input').size() + 1;
$('#add').click(function() {
$('<div><input type="text" class="field" name="dynamic[]" value="' + i + '" /></div>').fadeIn('slow').appendTo('.inputs');
i++;
});
$('#remove').click(function() {
if(i > 1) {
$('.field:last').remove();
i--;
}
});
$('#reset').click(function() {
while(i > 2) {
$('.field:last').remove();
i--;
}
});
// here's our click function for when the forms submitted
$('.submit').click(function(){
var answers = [];
$.each($('.field'), function() {
answers.push($(this).val());
});
if(answers.length == 0) {
answers = "none";
}
alert(answers);
return false;
});
});
Hola! I’ve been following your website for a long time now and finally got the courage to go ahead and give you a shout out from Lubbock Tx! Just wanted to say keep up the great job!
Also visit my weblog Immigration Solicitor folkstone
Pingback: Dynamically adding form inputs and submit using jQuery
You’ll encounter a change in your lifestyle after undergoing this bariatric surgery. Many surgeries can be expensive and can involve challenging lifestyle changes, particularly after lap-band surgery and other bariatric procedures. Then it was off to enrolling in the Silver Hill Psychiatric Hospital in New Canaan, Connecticut.
Its fine good.. but how can i validate these dynamically added fields.. can any one help me out…
I Feel post, “Dynamically adding form inputs and submit using jQuery | Papermashup.
com” was in fact spot on! I personallycannot see eye to
eye with u even more! Finally looks like I personallyuncovered a blog definitely worth
reading through. Thanks for your time, Heather
How to I make more than one of these boxes with different attributes so I can PHP post it individually?!?!
by the way, in order to validate this with PHP, use this:
<?php
foreach($_POST['dynamic'] as $value) {
echo "$value “; // change this to what you want to do with the data
}
?>
Hi,
What should i do if I want to delete/insert a row/element in the middle? I would like to have a + and – sign so that I can insert / delete element in the middle.
thx, wish you had told us how to retreive the information by PHP. I have a problem finding out how to send the values to a PHP page by POST, as you use the submit button for another purpose …
I saw the example of “Dynamically adding form inputs and submit using jQuery” and it is great, but i’d like to see another example how to add and remove input fields using AJAX and submit data into db.
Thanks.
very useful and very nice article thank you
Pingback: Dynamically adding form inputs and submit using jQuery | Tech Blog
Hi! Your scripts work fine. but this script only get value using javascript. I want how to get value using PHP for Dynamically adding form inputs.
very good script but would be better if it would validate the fields as well and also explain how to retrieve those textbox values from server side language like php.
i’ve found a tutorial at
http://www.pradipchitrakar.com.np/blog/dynamically-add-remove-textfield.html
it explains how to dynamically add, remove and validate text field in jquery and fetch the values of textbox in php and jsp/servlet
yeah..it’s work fine here…but i have the same question like João Carlos …how can i use form validation?on my script the validation rule only recognize the field with name=”dynamic” and not “dynamic[0]…bla..bla..bla”
need ur help soon..
thank u..
Use the array index.
field1 = dynamic[0]
field2 = dynamic[1]
and so on…
Hello, i’m using this tutorial to save in a mysql database. But i dont know how to save this added fields in different rows of a table. One row for each value.
Hi! Your scripts work fine here… But how can I use the validate and maskedinput JQuery scripts with this solution?
I need to validate de dynamic inputs and maybe put a mask on it. Maybe you can help!
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan. I also run Mega Infographics for your daily dose of the best infographics.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
17 discussions around Dynamically adding form inputs and submit using jQuery