Dynamic input field allows providing multiple values in a form. It is very useful when you want to receive multiple inputs for the same field in an HTML form. The dynamic input fields feature can be easily integrated using jQuery. You can add multiple fields and remove fields easily. In this tutorial, we will show you how to add or remove input fields or text boxes dynamically in a form using jQuery and get multiple input field values using PHP.
The example code shows how to generate input fields on the fly in a form and submit the input field’s value into the database. This script is very useful to add/remove input fields and post multiple values in PHP. The jQuery will be used to make this functionality simple and powerful. The following functionalities will be implemented to add more fields dynamically using jQuery and PHP.
In this example script, we will show you how to add more fields dynamically using jQuery.
JavaScript Code:
jQuery will be used to attach click events on add and remove buttons, so include the jQuery library first.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
The following JavaScript code handles the add/remove input field functionality using jQuery.
maxField
variable holds the maximum number of input fields to be added.addButton
variable selects the add button element.wrapper
variable selects the parent element of input fields.fieldHTML
variable holds the HTML of the new input field.maxField
, the new input field HTML will append into the fields parent div (wrapper
). Also, the field counter will increase.<script>
$(document).ready(function(){
var maxField = 10; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><a href="javascript:void(0);" class="remove_button"><img src="images/remove-icon.png"/></a></div>'; //New input field html
var x = 1; //Initial field counter is 1
// Once add button is clicked
$(addButton).click(function(){
//Check maximum number of input fields
if(x < maxField){
x++; //Increase field counter
$(wrapper).append(fieldHTML); //Add field html
}else{
alert('A maximum of '+maxField+' fields are allowed to be added. ');
}
});
// Once remove button is clicked
$(wrapper).on('click', '.remove_button', function(e){
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
x--; //Decrease field counter
});
});
</script>
HTML Code:
Initially, a single input field will display with the Add button. When the Add button is clicked, the duplicate input field HTML will appear with a Remove button.
<div class="field_wrapper">
<div>
<input type="text" name="field_name[]" value=""/>
<a href="javascript:void(0);" class="add_button" title="Add field"><img src="images/add-icon.png"/></a>
</div>
</div>
Once the multiple input form fields are added and submitted, the values can be retrieved using PHP. You can get the multiple values as an array using the $_POST method in PHP.
$field_values_array = $_POST['field_name'];
print_r($field_values_array);
// Output
Array
(
[0] => value1
[1] => value2
[2] => value3
[3] => value4
...
)
After you fetch the multiple input field values, use foreach loop to insert the values into the database or whatever you want to process further.
// Get multiple input field's value
$field_values_array = $_POST['field_name'];
foreach($field_values_array as $value){
// Your database query goes here
}
Add Remove Input Fields Group Dynamically with jQuery
Add more fields feature is very useful when you want to allow the user to add multiple inputs in web form. This example script helps you integrate add/remove multiple input fields functionality using jQuery. The user can add or remove fields dynamically and submit multiple values. Moreover, you can get values from multiple input fields easily using PHP.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Very nice, and thankful, it helps me much
Hi, if i want to have dynamic input fields for different type of data, how this is achieved? Is it in the same wrapper?
For example I want to get 2 input fields each time I click +, one to write a city and one to write an address
…
Then in PHP after submit I would save values for both arrays:
$field_city_array = $_POST[‘city_name’];
$field_address_array = $_POST[‘address_name’];
thankyou so much, i can insert to db. But the question now is, how to make edit form like this? is it possible? thank you..
Thanks very useful.
Hello,
Thanks for this snippet, it’s very helpfull.
I would like to know if we can do a think like this : when i click the add button, i got a increment variable in the name field like that : name=”field_name.$i” with jquery. Thanks for your help.
Awesome! God bless you!
Hi, great code, one question how can use this but in a grid for example a bills that ussually have more than one line and how can I sava the information to a Data BAse? thank you for you help
how to automatically view four column in starting time
Thanks for this post, i am looking at applying this in codeigniter
it worked in my project. thank you a lot . i really need to this code.
thanks for sharing us this is value information for thanks a lot
How can i add submit button to catch every field renerated?
how do you capture the array values in a java class then instead of PHP?
Hi,
How can i add like this and display the multiple series of textbox values.
can you please help me guys.
See this tutorial – https://www.codexworld.com/add-remove-input-fields-group-dynamically-jquery/
how can i do this with autocomplete textfields ,i want to add more fields with autocomplete textfields
Hello,
thanks for this nice script. there is just one thing i wish you help tell me how to solve this, How to limit the amount of fields to be added.
I wish to limit the fields to 6. How do i implement that?
thanks for your help.
The
maxField
option is used to set the maximum number of fields that could be added. Use the following line for your requirement.Great thing!, Please guide me how can I do this in the codeigniter? Help Me.
very useful.. thanks a lot for sharing 🙂
thanx
Hello. Thank you for the article.
the inclusion in the database is possible to be done with jquery / ajax?
if so, how do?
pls add example sample database query in foreach loop…
pls help me…
Thanks for the tutorial. But what do you do when you’re validating the data and there’s something bad. Assuming you’re validating only with PHP, is there any way not to lose all the fields you added with JavaScript?
can i get this php code in jsp
Thanks for sharing your knowledge.
Thanks for the article! this works for me 😀
For those who use in bootstrap,
it is better to put the HTML into a span element,
and in the remove section you can remove that span:
$(this).parents(‘span’).remove(); //Remove field html
regards!
it worked with my project, thank you! helped a lot.
How to do this inside Bootstrap Modal box
Good solution. Thanks for the article
Thanks for the article 🙂
Thank you so very much codexworld. I followed your example to the dot on my bootstrap and sent the array values to the mysql database and all worked like a charm after a full 2 days of struggling. I have seen a lot of examples but yours is the only one that worked for me. Thank you thank you very much.
how to edit addmore field
Thanks for nice article, but how to remove all input fields dynamically because I use that in modal bootstrap.
thanks 😀