Ad

Laravel Insert A Multiple Inputs

- 1 answer

i have issue in laravel 5.1 with insert a multiple inputs intro DB.

i build a query code for multiple inputs and after that i send them to controller but i have there a issues to insert it to DB

how i can insert the inputs to db if the results its like this:

{"_token":"C6m83bcZKaQsOtRiYEKxJAzzZjvdLerl9QpsvSSs","client_id":["aJQsijwqFVG9r0","aJQsijwqFVG9r0"],"short":["4","11"],"url":["567567567567","3453434534"]}

the code:

HTML:

                 <form class="js-validation-material form-horizontal push-10-t" action="{!! url() !!}/addreg" method="post">
                                {!! csrf_field() !!}
                                <div class="form-group">
                                    <div id="buildyourform"></div>
                                </div>
                                <div class="form-group">
                                    <div class="col-xs-12">
                                        <button class="btn btn-sm btn-primary" type="submit">Submit</button>
                                    </div>
                                </div> 
                </form>
                <script>
                $(document).ready(function() {
                $("#add").click(function() {
                    var intId = $("#buildyourform div.form").length + 1;
                    var fieldWrapper = $("<div class=\"form col-sm-6 col-lg-6\" id=\"field" + intId + "\"><div class=\"form-material\">");
                    var client_id = $("<input type=\"hidden\" name=\"client_id[]\" value=\"{!! $task->client_id !!}\" class=\"form-control\" />");
                    var langname = $("<select type=\"text\" name=\"short[]\" class=\"form-control\">{!! $data['langs'] !!}</select>");
                    var url = $("<input type=\"text\" name=\"url[]\" class=\"form-control\" placeholder=\"Insert a url..\" />");
                    var label = ("<label for=\"date\"><h3 class=\"block-title\">Lang" + intId + "</h3></label>");
                    var removeButton = $("<button class=\"btn btn-danger  btn-xs push-5-r push-10\" type=\"button\"><i class=\"fa fa-times\"></i></button>");
                    removeButton.click(function() {
                        $(this).parent().remove();
                    });

                    $("#buildyourform").append(fieldWrapper);
                    $("#field" + intId + " .form-material").append(client_id,langname,url,removeButton,label);

                });
            });
           </script>

the results need to be in the DB like:

    DB::table('table')->insert([
        ['client_id' => $request->client_id],
        ['short' => $request->short],
        ['url' => $request->url],
    ]); 

for each input.

Ad

Answer

The problem is you are sending multiples inputs with the same name, so it only get the last one.

Everytime you do and "add" try to wrap them in a div with a class (example: "foo"). So you will get some "foo" divs.

Create a hidden input called for example "items"

{{ Form::hidden('items') }}

Next you have to overwrite the click function of the submit button:

$('#submit-form').click(function(e){
    a = {};
    $.each($('.foo'), function(k,v){
        a[k] = $(v).find(':input').serializeArray()
    });
    $('input[name="items"]').val(JSON.stringify(a));
    $('#form').submit()
}

And in the controller:

$items = json_decode(Input::get('items'), true);    

Then you have an array with all of your inputs.

Ad
source: stackoverflow.com
Ad