简体   繁体   中英

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');

您的数据必须作为javascript对象发送,因此正确的语法应为

data: {name: input}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM