1

I want to save the multiple data to database with autoincremented id in the different column have a unique key for each data. User can dynamically add input fields and finally click submit button to save data in database with different id(auto incremented id) for each.

My js code id

    <script>
  $(document).ready(function(){
    $('#add').click(function(){
        var inp = $('#box');
        var i = $('input').size() + 1;
          $('<div id="box' + i +'"><input type="text" id="name" class="name" name="tagName' + i +'" placeholder="Input '+i+'"/><img src="<?php echo '../../uploads/remove.png'?>" width="32" height="32" border="0" align="top" class="add" id="remove" /> </div>').appendTo(inp);
        i++;
    });
    $('body').on('click','#remove',function(){
        
        $(this).parent('div').remove(); 
    });    
});
</script>

Form to insert data

<div class="row-fluid">
   <div class="span6">
     <div class="control-group">
         <label class="control-label">Add Tags<span class="required"></span></label>
           <div class="controls">
              <div id="box">        
                 <input type="hidden" name="_token" value="{{csrf_token()}}">
                  <input type="text" name="tagName[]" id="name" class="m-wrap span12" placeholder="Input Tags" 
                                                  value="">  
                     <a href="#" class="btn blue" id="add">Add More</a>
              </div>
            </div>
          </div>
        </div>
      </div>

Controller function:

 foreach( Input::get('tagName') as $name) {       
                    $objectTagProduct = new TagModel;
                    $objectTagProduct ->name = $name;
                    $objectTagProduct->save();
            }

I am able to insert only first data initially and now I am getting this error:

Invalid argument supplied for foreach()

1
  • Can you dump Input::get('tagName') just to see what it holds Commented Dec 29, 2017 at 11:46

2 Answers 2

1

I think the problem is here:

 $('<div id="box' + i +'"><input type="text" id="name" class="name" name="tagName' + i +'"...
                                                                                 ^^^^^^^^^

You have to use brackets if you want to get tagName as array:

 $('<div id="box' + i +'"><input type="text" id="name" class="name" name="tagName[]"...
Sign up to request clarification or add additional context in comments.

Comments

0

You can try to save below way.

$data[] = Input::get('tagName');

Print the $data and check record is arrive or not and then save below way.

foreach($data  as $name) {       
   $objectTagProduct = new TagModel;
   $objectTagProduct ->name = $name;
   $objectTagProduct->save();
}

1 Comment

I tried this method but i am able to get only the first data of text field. Still i am struggling to get all the data and save to database. I am also getting this error preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.