title: Dynamic Add or Remove input fields and save data to Database
body: <p>I found problem to run dynamic form in codeigniter, my problem after i submitted data in dynamic form, all data can't save data into database SQL, for more detail you can see my code here :</p>
  
  <p><strong>Views :</strong></p>
  
  <pre><code>&lt;html&gt;  
    &lt;head&gt;  
       &lt;title&gt;Form Data&lt;/title&gt;  
       &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /&gt;  
       &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"&gt;&lt;/script&gt;  
       &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"&gt;&lt;/script&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
      &lt;div class="container"&gt;  
        &lt;h2 align="center"&gt;Personal Information&lt;/h2&gt;  
        &lt;div class="form-group"&gt;  
          &lt;form name="add_name" id="add_name"&gt;  
            &lt;div class="table-responsive"&gt;  
              &lt;table class="table table-bordered" id="dynamic_field"&gt;  
                &lt;tr&gt;  
                 &lt;td&gt;&lt;input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" style="width:100px;"/&gt;&lt;/td&gt;  
                 &lt;td&gt;&lt;input type="text" name="age[]" placeholder="Enter your Age" class="form-control name_list" style="width:100px;"/&gt;&lt;/td&gt; 
                 &lt;td&gt;&lt;input type="remark" name="remark[]" placeholder="Remark" class="form-control name_list" style="width:100px;"/&gt;&lt;/td&gt;   
                 &lt;td&gt;&lt;button type="button" name="add" id="add" class="btn btn-success"&gt;Add More&lt;/button&gt;&lt;/td&gt;  
                &lt;/tr&gt;  
              &lt;/table&gt;  
              &lt;input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" /&gt;  
            &lt;/div&gt;  
          &lt;/form&gt;  
        &lt;/div&gt;  
      &lt;/div&gt;  
    &lt;/body&gt;  
   &lt;/html&gt;  
   &lt;script&gt;  
   $(document).ready(function(){  
        var i=1;  
        $('#add').click(function(){  
             i++;  
             $('#dynamic_field').append('&lt;tr id="row'+i+'"&gt;&lt;td&gt;&lt;input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" style="width:100px;"/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="age[]" placeholder="Enter your Age" class="form-control name_list" style="width:100px;"/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="remark" name="remark[]" placeholder="Remark" class="form-control name_list" style="width:100px;"/&gt;&lt;/td&gt;&lt;td&gt;&lt;button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove"&gt;X&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;');  
        });  
        $(document).on('click', '.btn_remove', function(){  
             var button_id = $(this).attr("id");   
             $('#row'+button_id+'').remove();  
        });  
        $('#submit').click(function(){            
             $.ajax({  
                  url:"&lt;?php echo base_url(); ?&gt;dashboard_admin/save_item",  
                  method:"POST",  
                  data:$('#add_name').serialize(),  
                  success:function(data)  
                  {  
                       alert(data);  
                       $('#add_name')[0].reset();  
                  }  
             });  
        });  
   });  
   &lt;/script&gt;
  </code></pre>
  
  <p><strong>Controller :</strong></p>
  
  <pre><code>    public function save_item() 
  {
       $number = count($this-&gt;input-&gt;post["name"]);  
       if($number &gt; 0)  
       {  
            for($i=0; $i&lt;$number; $i++)  
            {  
                 if(trim($this-&gt;input-&gt;post["name"][$i] != ''))  
                 {  
                      $in['name'] = mysqli_real_escape_string($this-&gt;input-&gt;post["name"][$i]);
                      $in['age'] = mysqli_real_escape_string($this-&gt;input-&gt;post["age"][$i]);
                      $in['remark'] = mysqli_real_escape_string($this-&gt;input-&gt;post["remark"][$i]);
                      $this-&gt;db-&gt;insert("tbl_item",$in); 
                 }  
            }  
            echo "Data Inserted";  
       }  
       else  
       {  
            echo "Please Enter Name";  
       } 
  }
  </code></pre>
  
  <p>I already tried to run this code but nothing happen, if there any correction from my source code, i need your advice, thanks.</p>