Laravel表格提交给500

时间:2019-12-13 03:04:29

标签: jquery ajax laravel bootstrap-modal

我使用了引导程序模式框来提交表单。这是我的HTML

<div class="modal-body">
    <div class="card-body">
        <form action="" method="POST" id="customerSubmit">
            @csrf
            <div class="row">
                <div class="col">
                    @if(session('success'))
                    <div class="alert alert-success">{{session('success')}}</div>
                    @endif
                    @if(session('error'))
                    <div class="alert alert-error">{{session('error')}}</div>
                    @endif

                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="name">Name</label>
                            <input class="form-control" name="name" id="name" type="text" placeholder="Enter Name" data-original-title="" title="" required>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="hpcontact">HP Contact No</label>
                            <input class="form-control" name="hpcontact" id="hpcontact" type="text" placeholder="HP Contact No" data-original-title="" title="" required>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="icno">IC No</label>
                            <input class="form-control" name="icno" id="icno" type="text" placeholder="Enter IC No" data-original-title="" title="" required>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="homephone">Home Contact No</label>
                            <input class="form-control" name="homephone" id="homephone" type="text" placeholder="Enter home contact no" data-original-title="" title="" required>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="dob">D.O.B</label>
                            <input class="form-control" name="dob" id="dob" type="date" placeholder="Enter DOB" data-original-title="" title="" required>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="officeno">Office No</label>
                            <input class="form-control" name="officeno" id="officeno" type="text" placeholder="Enter Office No" data-original-title="" title="" required>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="address">Address</label>
                            <input class="form-control" name="address" id="address" type="text" placeholder="Enter Address" data-original-title="" title="" required>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="email">Email</label>
                            <input class="form-control" name="email" id="email" type="email" placeholder="Enter Email" data-original-title="" title="" required>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-primary" id="addcustomer" type="submit" data-original-title="" data-token="{{ csrf_token() }}" title="">Add Customer</button>
    <input class="btn btn-light" type="reset" value="Cancel" data-original-title="" title="">
</div>

这是表单提交的路线

Route::post('modaluser/add', 'CustomerController@adduser')->name('customer.adduser');

这是控制器和方法

public function adduser(Request $request)
{
    $input = $request->all();
    return Response()->json($input);
}

这是Ajax的jQuery

$(document).ready(function(){
    $('body').on('click','#addcustomer',function(e){
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        // AJAX request
        $.ajax({
            method: 'post',
            url: '/admin/modaluser/add',
            data: $('#customerSubmit').serialize(),
            success: function(msg) {
                console.log(msg);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log("some error");
            }
        });
    });
});

这是我要提交此https://eu.2e.my/admin/booking/choose的来源。选择包裹并提交随机数后,您可以在每一行中找到“ +添加客户”链接。只需单击其中任何一个即可。之后,该表格将出现。我已经给出了上面的代码。现在,当我提交此文件时,它给了我500错误。

谢谢。

2 个答案:

答案 0 :(得分:1)

ajax网址不正确

更改

    url: '/admin/modaluser/add',

url: '{{ url('modaluser/add') }}'

url: '{{ route('customer.adduser') }}'

答案 1 :(得分:0)

将数据保存到数据库

public function adduser(Request $request)
{
    $input = $request->all();
    ModelName::create($input);
    return Response()->json($input);
}