YouTube Icon

Add Remove Input Fields Dynamically using jQuery




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 fields value 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.

JavaScript Code:

Include the jQuery library to use click event for add and remove buttons.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Also Read:- How to Create Multiplication Table Using JavaScript

The following JavaScript code handle adds remove input field functionality.

  • 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 maximum input fields 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 would be incremented.
  • Once the Remove button is clicked, the parent div of that particular remove button will be removed. Also, the field counter would be decrement.
<script type="text/javascript">
$(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="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++; //Increment field counter
            $(wrapper).append(fieldHTML); //Add field html
        }
    });
    
    //Once remove button is clicked
    $(wrapper).on('click', '.remove_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });
});
</script>

HTML Code:

Initially, one input field will display with Add button. When the add button is clicked, the duplicate input field will appear with the 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="add-icon.png"/></a>
    </div>
</div>

Get Multiple Values in 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 $_POST in PHP.

Also Read:- How to build Snake using only JavaScript, HTML & CSS: Think like a Developer

$field_values_array = $_POST['field_name'];

print_r($field_values_array);
//output
Array
(
    [0] => value1
    [1] => value2
    [2] => value3
    [3] => value4
)
?>

After you fetched the multiple input fields values, use foreach loop to insert the values into the database or whatever you want.

// Get multiple input field's value 
$field_values_array = $_POST['field_name'];

foreach($field_values_array as $value){
    // Your database query goes here
}

 



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink

Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala
CFT

News & Blogs

00a20a805c81004bbaf4c775beb453e4.png

How to Add Spell Checker In Your Webpage Using ...

We have collected Best jQuery Spell Checker Plugins which allows you to check spelling. If you ar...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh
CFT

Our Client Says

WhatsApp Chat with Our Support Team