0

I'm building a form with Bootstrap in a Laravel 5.2 view, where I will have an option to add additional input fields like you can see here in this link.

So, I will essentially need to be passing a PHP array of data (I think) to my controller. I'm just trying to comprehend how exactly I can do this.

2 Answers 2

1

html:

 <div id="dynamicInput">
       <span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
 </div>

javascripts:

function addInput(divName)
{ 
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<input type='text' name='items[]'>";
          document.getElementById(divName).appendChild(newdiv);    
}

you can have array of items this way.

and in your controller to save these.

 foreach($items as $item)
    {
     if(!empty($item))
      {     
        $add=new Item();   //Item is the model
        $add->name=$item;  //saving item to name column
        $add->save();       
      }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh yes, this will be perfect. Thank you for your help!
1

You can do this using ajax ( jQuery )

$.ajax({
    url: 'your/array/route',
    type: 'GET',
    success: function(data) {

        if(data.result != null) {
            $.each(data.result, function(i, v) {
                $("#form").append('<input type="text" value="'+v.your_value+'">');
            );
        }
    }
});

Comments

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.