draw134
draw134

Reputation: 1187

Cant insert into table using modal and ajax in laravel

I cant insert a into my database using bootstrap modal and some ajax. Can someone help me. I recieved an error of Uncaught SyntaxError: Invalid shorthand property initializer. I've been looking for some similar problems with this but I cant find it. Im new to AJAX by the way so im sorry. Does anyone know how to fix this. TIA

Modal

          <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                  <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content">
                      <div class="modal-header"> 
                        <h5 class="modal-title" id="exampleModalCenterTitle">Supplier</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                      <p style="font-weight: bold;">Name </p>
                        <input  style="text-transform:uppercase"  type="text" class="form-control" id="supplier"/>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" id="add_supplier">Add</button>
                      </div>
                    </div>
                  </div>
                </div> 

Ajax


            <script type="text/javascript">

            $(document).ready(function(){

                $('#add_supplier').click(function(e){
                    e.preventDefault();
                    var input = $('#supplier').val();
                    $.ajaxSetup({
                        headers:{
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });

                    $.ajax({
                        url: "{{    url('/supplier')    }}",
                        method: 'post',
                        data: {
                            name = input
                        },
                        success: function (res){
                            console.log(res);
                            window.location.href = '{{route("supplier.index")}}';
                        }
                    });
                });

            });



            </script>

Controller

    public function store(Request $request)
    {
        $data = $request->all();

        $data['name'] = ($data['name']);

        Supplier::create($data);

        return response()->json($data);
    }

route

Route::resource('supplier', 'SupplierController');

Upvotes: 0

Views: 127

Answers (1)

aynber
aynber

Reputation: 23034

Your data must be sent as a javascript object, so the correct syntax would be

data: {name: input}

Upvotes: 1

Related Questions