Add field dynamically in forms and glitches via request - Laravel

0

Good evening guys, I am creating a form with Laravel 5.5 and the user can add fields dynamically, but the Controller is not recognizing the fields added after the page loads.

        <div class="box-body">
          <table class="table table-bordered table-hover">
            <form method="POST" action="{{ route('save.orcamento') }}">
            <!-- Token oculto -->
            <tr>
            {!! csrf_field() !!}
            </tr>

            <tr class="produtos">

            </tr>
            <button type="submit" class="btn btn-primary">Criar</button>
            </form>
            <br><br>
          </table>
        </div>

$(document).ready(function() {
var max_fields      = 10; //maximum input boxes allowed
var wrapper         = $(".produtos"); //Fields wrapper
var add_button      = $(".addprod"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
    e.preventDefault();
    if(x < max_fields){ //max input box allowed
        x++; //text box increment
        $(wrapper).append('<div><input type="text" name="name['+x+']"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
    }
});

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
    e.preventDefault(); $(this).parent('div').remove(); x--;
})
});

Controller code:

 public function save(Request $request)
 {
    for ($i = 0; $i < count($request->name); $i++) 
    { 
         $this->product->create([
                    'name' => $request->name[$i],
         ]);
    } 
 }

Someone could help me, I'm 2 in this problem and I can not get over it.

Thank you

    
asked by anonymous 22.05.2018 / 22:58

0 answers