[英]Submitting a form with ajax and laravel
I'm trying to add product to the database with Ajax without refreshing the page and send the data to the database but I get an error Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'.
我试图在不刷新页面的情况下使用 Ajax 将产品添加到数据库并将数据发送到数据库,但出现错误
Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'.
on console.在控制台上。 How can I submit the form without refreshing the page?
如何在不刷新页面的情况下提交表单?
Blade刀
<form method="POST" role="form" enctype="multipart/form-data">
{{csrf_field()}}
<label for="pro_name">Name</label>
<input type="text" class="form-control" name="pro_name" id="pro_name" placeholder="Enter product name">
<label for="category_id">Choose Category</label>
<select name="category_name" id="category_name">
<option value=""> --Select Category -- </option>
@foreach ($categoryname_array as
$data)
<option value="{{ $data->name }}" >{{$data->name}}</option>
@endforeach
</select>
<label for="photos">Choose 5 Images</label>
<input "multiple="multiple" name="photos[]" type="file">
<button type="button" onclick = "submitThisForm({{Auth::user()->id}})" class="btn btn-primary">Submit</button>
</form>
Ajax阿贾克斯
<script>
function submitThisForm(id){
let url = "{{ route('product.store', ['id' => ':id']) }}".replace(':id', id);
$.ajax( {
url: url,
type: 'POST',
data: new FormData( this ),
processData: false,
contentType: false,
success: function(result){
console.log(result);
}
} );
e.preventDefault();
}
</script>
Route路线
Route::post('seller/product', 'ProductController@store')->name('product.store');
I think you're overcomplicating things.我认为你把事情复杂化了。
First, your route does not expect any parameters.首先,您的路线不需要任何参数。
Route::post('seller/product', 'ProductController@store')->name('product.store');
So you don't need to pass it on.所以你不需要传递它。
{{ route('product.store', ['id' => ':id']) }} // remove , ['id' => ':id']
Then, since you are using jquery, you can handle the ajax call on the submit method of the form.然后,由于您使用的是 jquery,因此可以处理对表单提交方法的 ajax 调用。
$('form').on('submit', function (e) {
e.preventDefault(); // prevent the form submit
var url = '{{ route('product.store' }}';
// create the FormData object from the form context (this),
// that will be present, since it is a form event
var formData = new FormData(this);
// build the ajax call
$.ajax({
url: url,
type: 'POST',
data: formData,
success: function (response) {
// handle success response
console.log(response.data);
},
error: function (response) {
// handle error response
console.log(response.data);
},
contentType: false,
processData: false
});
})
In your form you will not need the onclick event on the button..在您的表单中,您不需要按钮上的 onclick 事件。
<form method="POST" role="form" enctype="multipart/form-data">
{{csrf_field()}}
<label for="pro_name">Name</label>
<input type="text" class="form-control" name="pro_name" id="pro_name" placeholder="Enter product name">
<label for="category_id">Choose Category</label>
<select name="category_name" id="category_name">
<option value=""> --Select Category -- </option>
@foreach ($categoryname_array as $data)
<option value="{{ $data->name }}" >{{$data->name}}</option>
@endforeach
</select>
<label for="photos">Choose 5 Images</label>
<input "multiple="multiple" name="photos[]" type="file">
<button type="button" class="btn btn-primary">Submit</button>
</form>
Try this.试试这个。
<form id="myForm" method="POST" role="form" enctype="multipart/form-data">
<script>
function submitThisForm(id){
let url = "{{ route('product.store', ['id' => ':id']) }}".replace(':id', id);
var form_data = new FormData(document.getElementById("myForm"));
$.ajax( {
url: url,
type: 'POST',
data: form_data,
processData: false,
contentType: false,
success: function(result){
console.log(result);
}
} );
e.preventDefault();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.