Add Remove Input Fields Dynamically using jQuery

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.

  • Add and remove input fields dynamically using jQuery.
  • Get multiple input field values using PHP.

Add / Remove Input Fields Dynamically using jQuery

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.

  • The maxField variable holds the maximum number of input fields to be added.
  • The addButton variable selects the add button element.
  • The wrapper variable selects the parent element of input fields.
  • The fieldHTML variable holds the HTML of the new input field.
  • Once the Add button is clicked, it will check the maximum input field limit. If field counter (x) is less than maxField, the new input field HTML will append into the fields parent div (wrapper). Also, the field counter will increase.
  • Once the Remove button is clicked, the parent div of that particular Remove button will be removed. Also, the field counter will decrease.
<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>

Get Values from Multiple Input Fields using PHP

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

Conclusion

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.

Are you want to get implementation help, or modify or enhance the functionality of this script? Click Here to Submit Service Request

If you have any questions about this script, submit it to our QA community - Ask Question

36 Comments

  1. Anjum Said...
  2. Kukoc Said...
  3. Yeoppeottah Said...
  4. Ajay Said...
  5. Moho Said...
  6. Joseph Adegbola Said...
  7. Jamarchi Said...
  8. Vivek Said...
  9. Okhale Abraham Said...
  10. Design Said...
  11. Hanumanbabu Said...
  12. Betino Pagula Said...
  13. Ayu Said...
  14. Navas Said...
  15. Sharad Said...
  16. Victor Said...
    • CodexWorld Said...
  17. Aasif Said...
  18. Deepika Verma Said...
  19. Swapna Said...
  20. Bruno Serrano Said...
  21. Hariram Said...
  22. Ruben Said...
  23. Aruna Said...
  24. Vikram Said...
  25. Vascijs Said...
  26. Jjozsi Said...
  27. Jjozsi Said...
  28. GUNASEGAR Said...
  29. Ankur Kukreja Said...
  30. Blacmoon Said...
  31. Goodbyeplanet Said...
  32. Shubh Said...
  33. Eko Wahyudi Said...
  34. Factor Said...

Leave a reply

keyboard_double_arrow_up