In his life, he only encountered an ajax 2 times and here again our paths intertwined with him and he gave me 422 errors. I googled and realized that 422 error is a validation error, but in what specific place the error, I just can not understand.
This is my script
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$( "#form" ).submit(function( e ) {
e.preventDefault();
var message = $('#message').val();
var img = $('#img').val();
var user_id = $('#user_id').val();
$.ajax({
type: "POST",
url: "{{route('profile.store', ['id' => Auth::user()->id])}}",
data: {message:message, img:img, user_id:user_id},
success: function (data) {
$("#textpost").html($(data).find("#textpost").html());
},
});
});
</script>
And my method
public function store(Request $request) {
$validator = $this->validate($request,[
'message' => 'required|max:1000',
'img' => 'mimes:jpeg,png,gif|max:3000',
]);
if($validator ) {
$post = new Profile();
$post->message = $request->message;
$post->user_id = Auth::user()->id;
if($request->file('img')) {
$path = Storage::putFile('public', $request->file('img'));
$url = Storage::url($path);
$post->img = $url;
}
$post->save();
}
return redirect()->back();
}
And my form
<div class="card-header">
<div class="input-group">
<form action="{{route('profile.store', ['id' => Auth::user()->id])}}" method="post" enctype="multipart/form-data" id="form">
#csrf
<textarea class="form-control" id="message" name="message" cols="100" rows="4" placeholder="О чем думаешь?"></textarea>
<input type="file" id="img" name="img" value="Прикрепить изображение" class="mt-2">
<div class="text-right">
{{-- <input type="submit" id="btn" value="Отправить" class="btn btn-outline-dark btn-sm mt-4">--}}
<button type="submit" id="btn" class="btn btn-outline-dark btn-sm mt-4">Отправить</button>
</div>
</form>
</div>
</div>
$validator = $this->validate($request,[
'message' => 'required|max:1000',
'img' => 'mimes:jpeg,png,gif|max:3000',
]);
This portion of your code throws a validation errors exception which returns 422.
If you wish to handle validation on your own, try this logic instead:
$validator = validator($request->all(), [
'message' => 'required|max:1000',
'img' => 'mimes:jpeg,png,gif|max:3000',
]);
if($validator->fails()) {
// return as appropriate
return response()->son($validatior->errors(), 422);
}
// rest of your code
There is a problem with your Ajax call, because you are uploading text and file at the same time you can pass it through FormData. You were also missing
contentType: false,
processData: false,
Here is the updated ajax call:
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$( "#form" ).submit(function( e ) {
e.preventDefault();
var formData = new FormData(this); //set formData to selected instance
var message = $('#message').val();
var img = $('#img').val();
var user_id = $('#user_id').val();
$.ajax({
type: "POST",
url: "{{route('profile.store', ['id' => Auth::user()->id])}}",
data: formData, //pass to our Ajax data to send
success: function (data) {
$("#textpost").html($(data).find("#textpost").html());
},
contentType: false,
processData: false,
});
});
</script>
Setting processData makes it so your FormData is not converted to a string. Setting contentType to false is used for forms that pass files, when false, no header will be added to the request, which is exactly what we want when submitting multipart/form-data.
Related
I am trying to upload an image using ajax in Laravel, but when I do, I receive this message Call to a member function getClientOriginalExtension() on null. I don't know if there is something wrong with my code. Help!
ProductController
This is where I try to send the image.
public function store(Request $request)
{
$validator = Validator::make($request->input(), array(
'name' => 'required',
'category_id' => 'required',
'description' => 'required',
'price_neto' => 'required',
'iva' => 'required',
'price_total' => 'required',
'image' => 'required|image',
));
$productImage = $request->file('image');
$productImageName = time() . $productImage->getClientOriginalExtension();
$productImage->move(public_path("img/products"), $productImageName);
if ($validator->fails()) {
return response()->json([
'error' => true,
'messages' => $validator->errors(),
], 422);
}
$products = Product::create($request->all());
return response()->json([
'error' => false,
'products' => $products,
], 200);
}
Product.js
This is my Product.js file. It works correctly but now I need to add the image to the product.
$(document).ready(function() {
$("#btn-add").click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'POST',
url: '/product',
data: {
name: $("#frmAddProduct input[name=name]").val(),
category_id: $("#frmAddProduct select[name=category_id]").val(),
description: $("#frmAddProduct input[name=description]").val(),
price_neto: $("#frmAddProduct input[name=price_neto]").val(),
iva: $("#frmAddProduct input[name=iva]").val(),
price_total: $("#frmAddProduct input[name=price_total]").val(),
image: $("#frmAddProduct input[name=image]").val(),
},
dataType: 'json',
success: function(data) {
$('#frmAddProduct').trigger("reset");
$("#frmAddProduct .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#add-product-errors').html('');
$.each(errors.messages, function(key, value) {
$('#add-product-errors').append('<li>' + value + '</li>');
});
$("#add-error-bag").show();
}
});
});
});
function addProductForm() {
$(document).ready(function() {
$("#add-error-bag").hide();
$('#addProductModal').modal('show');
});
}
Product.blade.php
<div class="modal fade" id="addProductModal">
<div class="modal-dialog">
<div class="modal-content">
<form id="frmAddProduct">
<div class="modal-header">
<h5 class="modal-title">
<span class="fw-mediumbold">New</span>
<span class="fw-light"> Product</span>
</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="alert alert-danger" id="add-error-bag">
<ul id="add-product-errors"></ul>
</div>
<div class="row">
{{-- another code --}}
<div class="col-md-6">
<div class="form-group">
<label>Imagen</label>
<input class="form-control-file" id="image" name="image" required="" type="file">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="btn-add" class="btn btn-primary">Add</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
You can't pass an image through ajax by getting the element's value. Also, it's convenient and preferred to use FormData object to send files to the server via ajax.
So, try this instead:
// Select the image holding element.
var productImage = document.querySelector('#frmAddProduct input[name=image]');
// Creating an instance of FormData to submit the form.
var formData = new FormData();
formData.append('name', $("#frmAddProduct input[name=name]").val());
formData.append('category_id', $("#frmAddProduct select[name=category_id]").val());
formData.append('description', $("#frmAddProduct input[name=description]").val());
formData.append('price_neto', $("#frmAddProduct input[name=price_neto]").val());
formData.append('iva', $("#frmAddProduct input[name=iva]").val());
formData.append('price_total', $("#frmAddProduct input[name=price_total]").val());
formData.append('image', productImage.files[0]);
$.ajax({
type: 'POST',
url: '/product',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function(data) {
$('#frmAddProduct').trigger("reset");
$("#frmAddProduct .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#add-product-errors').html('');
$.each(errors.messages, function(key, value) {
$('#add-product-errors').append('<li>' + value + '</li>');
});
$("#add-error-bag").show();
}
});
You need to use FormData:
$("#btn-add").click(function(){
var formData = new FormData($("#frmAddProduct")[0]);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'POST',
url: '/product',
data: formData,
dataType: 'json',
success: function(data) {
$('#frmAddProduct').trigger("reset");
$("#frmAddProduct .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#add-product-errors').html('');
$.each(errors.messages, function(key, value) {
$('#add-product-errors').append('<li>' + value + '</li>');
});
$("#add-error-bag").show();
}
});
});
This is my code after some corrections.
Product Controller
public function store(Request $request)
{
$validator = Validator::make($request->input(), array(
'name' => 'required',
'category_id' => 'required',
'description' => 'required',
'price_neto' => 'required',
'iva' => 'required',
'price_total' => 'required',
'image' => '',
));
$productImage = $request->file('image');
$productImageName = rand() . '.' . $productImage->getClientOriginalExtension();
$productImage->move(public_path('img/products'), $productImageName);
if ($validator->fails()) {
return response()->json([
'error' => true,
'messages' => $validator->errors(),
], 422);
}
$products = Product::create([
'name' => $request->name,
'category_id' => $request->category_id,
'description' => $request->description,
'price_neto' => $request->price_neto,
'iva' => $request->iva,
'price_total' => $request->price_total,
'image' => $productImageName,
]);
return response()->json([
'error' => false,
'products' => $products,
], 200);
}
Product.js
$("#btn-add").click(function() {
var formData = new FormData($("#frmAddProduct")[0]);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'POST',
url: '/product',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function(data) {
$('#frmAddProduct').trigger("reset");
$("#frmAddProduct .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#add-product-errors').html('');
$.each(errors.messages, function(key, value) {
$('#add-product-errors').append('<li>' + value + '</li>');
});
$("#add-error-bag").show();
}
});
});
And in my form, I just added enctype= "multipart/form-data".
Thanks!
Hi I am trying to create a post using form data, because I have a file input to upload images. I am really struggling with getting the upload file to work. I get an error 500 when I try to create a post, with the message
SQLSTATE[HY000]: General error: 1364 Field 'title' doesn't have a default value (SQL: insert into posts ('updated_at', 'created_at') values (2018-01-30 12:31:18, 2018-01-30 12:31:18).
I am not sure what is meant by default value. I checked my $fillables and title is definitely there. When I run the following code in my controller right at the top it passes:
$request->all();
return response()->json('passed');
But when I have the following in my controller it gives the error:
public function create(Request $request)
{
Post::create($request->all());
$response = [
'response' => 'Post Created Successfully',
'error' => 'Something went wrong'
];
return response()->json($response);
}
Here is my HTML:
<div class="container">
<div class="row">
<h1>Create your post</h1>
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" class="form-control">
</div>
<div class="form-group">
<label for="post">Post</label>
<textarea name="post" rows="8" cols="80" id="post" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="image">Add image</label>
<input type="file" name="image" id="image" class="form-control">
</div>
<input type="submit" name="submit" value="Submit Post" id="submit" class="btn btn-primary">
</div>
</div>
AJAX:
<script>
$(document).ready(function() {
$("#submit").on("click", function(e) {
e.preventDefault();
var formData = new FormData();
var fileData = $('#image').prop('files')[0];
var title = $('#title').val();
var post = $('#post').val();
formData.append('fileData', fileData);
formData.append('title', title);
formData.append('post', post);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr("content")
}
});
$.ajax({
url:'/post/create/create',
type: "POST",
data: {
formData: formData
},
processData: false,
contentType: false,
dataType: 'json',
success:function(response){
toastr.success(response.response);
},
error: function(error){
toastr.error(error.error)
}
});
});
});
</script>
Here is my Post model with the fillables:
protected $fillable = ['title', 'post', 'user_id', 'uploadImage'];
In-order to obtain the data from your ajax in your controller, pass the data as
data: formData
instead of
data: {
formData: formData
}
and you could process the information in your controller as
public function create(Request $request)
{
$path = '';
if ($request->hasFile('fileData')) {
if ($request->file('fileData')->isValid()) {
// Upload the file
$path = $request->fileData->store('your directory');
}
}
$post = Post::create([
'title' => $request->title,
'post' => $request->post,
'user_id' => auth()->user()->id,
'uploadImage' => $path
]);
if ($post) {
return response()->json([
'response' => 'Post Created Successfully!'
]);
}
return response()->json([
'error' => 'Something went wrong!'
], 500)
}
Documentation on storing uploaded files
I have form to POST data to Controller and update database
The form in my orders.blade.php
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<select name="selectdb" required>
<option value="" disabled selected>Select Delivery Boy</option>
#foreach($delvery_boys as $delvery_boy)
<option name="selectdb" data-oid="{{$order->id}}" value="{{$delvery_boy->id}}">{{$delvery_boy->name}}</option>
#endforeach
</select>
<button type="submit" class="assigndb-btn btn-floating waves-effect waves-light">
<i class="material-icons">send</i>
</button>
</form>
I am doing an ajax POST request of form data to controller in my orders.blade.php
$(document).one("click", ".assigndb-btn", function () {
$('form').submit(function(event) {
event.preventDefault();
var order_id = $(this).find(":selected").data('oid');
var delivery_boy_id = $(this).find(":selected").val();
var delivery_boy_name = $(this).find(":selected").text();
$.ajax({
url: '{{ url('/manager/assign_orders') }}',
type: 'POST',
data: {"order_id":order_id,"delivery_boy_id":delivery_boy_id},
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
});
});
And in my OrdersController.php i have the logic to updated the posted data
public function assignDeliveryBoy(Request $request)
{
$assign_delivery_boy = Order::where('id', $request->order_id)->update(['delivery_boy_id' => $request->delivery_boy_id]);
$data = [
'success' => true,
'message' => 'Order has been asigned'
];
return response()->json($data);
}
My Route is
Route::group(['prefix' => 'manager', 'middleware' => ['auth','roles'], 'roles' => 'manager'], function() {
Route::post('/assign_orders', 'OrdersController#assignDeliveryBoy')->name('assignOrder');
});
When i submit the form it suppose to hit the assign_order route and update the database
But in my console i am getting the html code of the page from where i submit the form basically it is executing GET instead of POST
As i checked in browser network whent the response is
Request URL:http://localhost:8000/manager/orders //but i am posting to http://localhost:8000/manager/assign_orders
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8000
Referrer Policy:no-referrer-when-downgrade
i really don't understand what is wrong
thank you
Try this code.
$.ajax({
url: "{{ url('/manager/assign_orders') }}",
type: 'POST',
data:{
"order_id":order_id,
"delivery_boy_id":delivery_boy_id,
'_token':'{{ csrf_token() }}'
},
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
Let me know if it works.
Try this:
$.ajax({
url: '{{ route('assignOrder') }}',
type: 'POST',
data: {"order_id":order_id,"delivery_boy_id":delivery_boy_id},
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
$.ajax({
url: '{{ route("assignOrder") }}',
type: 'POST',
data: {"order_id":order_id,"delivery_boy_id":delivery_boy_id,'_token':'{{ csrf_token() }}'},
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
Hello I'm using Codeigniter 3 and jQuery ajax.
I'm using the built in upload library...
I want to upload image on my server, but always get this error message:
You did not select a file to upload.
Here is my code
View
<?php echo form_open_multipart('settings/uploadprofilephoto', array('id' => 'upload-avatar-form'));?>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Upload profile photo</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<div class="row">
<div class="form-group col-md-6">
<input type="file" name="profilephoto" id="profile-photo" class="form-control">
</div>
<div class="form-group col-md-6">
<button type="submit" id="upload" class="btn btn-success">Upload</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Modal -->
<?php echo form_close();?>
Controller
public function uploadProfilePhoto(){
$config = array(
'upload_path' => base_url() . 'uploads/test',
'allowed_types' => 'jpg|jpeg|gif|png',
'min_height' => 480,
'min_width' => 640,
'remove_spaces' => true,
);
$this->load->library('upload', $config);
if($this->upload->do_upload("profilephoto")){
$data = array(
'status' => true,
'messages' => 'Uploaded'
);
echo json_decode($data);
}else{
$data = array(
'status' => false,
'messages' => $this->upload->display_errors()
);
echo json_encode($data);
}
}
ajax
/*
Upload profile photo
*/
$("#upload-avatar-form").submit(function(event){
$.post(base_url + "settings/uploadprofilephoto" , $(this).serialize(), function(data){
console.log(data);
//alert("ok");
});
event.preventDefault();
});
Where am I wrong?
serialize() will not pass image within it. It does not work with multipart formdata.
Instead use like this:
var formData = new FormData(this);
Pass this formData variable instead of $(this).serialize()
Try this
$('#button_name').on('click', function(event) {
event.preventDefault();
$.ajax({
url: "<?php echo base_url('settings/uploadprofilephoto');?>",
type: 'post',
dataType: 'json',
data: new FormData(this),
cache: false,
contentType: false,
processData: false,
success: function(json) {
// Success Stuff
},
});
});
On the view part
<button type="button" id="button_name">Upload</button>
You have to try this
$('#logo_form').on('submit',function(form){
form.preventDefault();
var me = $(this);
var file_data = $('#file').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: me.attr('action'), // point to server-side controller method
dataType: 'text', // what to expect back from the server
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (response) {
$("#logo_form")[0].reset();
$('#logo_success').html(response); // display success response from the server
window.setTimeout(function(){location.reload()},1000);
},
error: function (response) {
$('#error').html(response); // display error response from the server
}
});
});
Please check below mentioned solution, This will help you to send file with input data.
var myFormData = new FormData();
$(document).on("click", "button", function(e) {
e.preventDefault();
var inputs = $('#my_form input[type="file"]');
$.each(inputs, function(obj, v) {
var file = v.files[0];
var filename = $(v).attr("data-filename");
var name = $(v).attr("id");
myFormData.append(name, file, filename);
});
var inputs = $('#my_form input[type="text"]');
$.each(inputs, function(obj, v) {
var name = $(v).attr("id");
var value = $(v).val();
myFormData.append(name, value);
});
var xhr = new XMLHttpRequest;
xhr.open('POST', '/echo/html/', true);
xhr.send(myFormData);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="my_form" enctype="multipart/form-data">
<input type="file" name="file_1" id="file_1" data-filename="image.jpg"><br />
<input type="text" name="check1" id="check1"/><br />
<input type="text" name="check2" id="check2"/><br />
<input type="text" name="check3" id="check3"/><br />
<button>Submit</button>
</form>
Let me know if it not works.
I try on my laravel project to create a form that upload a image on the server using ajax but the request always failed, I try to do this is there any problem in code ?
html:
<form id="changeImage" enctype="multipart/form-data">
<input type="hidden" name="_token" id="_token" value="{{csrf_token() }}">
<center>
<div class="form-group">
<center><input type="file" name="image" accept="image/*"></center>
<p class="help-block">Format: png, jpg et gif.</p>
</div>
</center>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Ajax:
$('input[name="image"]').change(function(event) {
files = event.target.files;
});
$('#changeImage').submit(function(event) {
event.preventDefault();
var _token= $('input[name="_token"]').val();
var data = new FormData();
data.append('_token',_token);
$.each(files, function(k, v) {
data.append('image',v);
});
$.ajax({
url: '/profile/image',
type: 'POST',
data:data,
processData: false,
contentType: "multipart/form-data",
success: function(data){
console.log('done'+data);
},
async: false,
error: function(data){
console.log('No');
var errors = data.responseJSON;
errorsHtml = '<div class="alert alert-danger"><ul>';
$.each( errors , function( key, value ) {
errorsHtml += '<li>' + value[key] + '</li>';
});
errorsHtml += '</ul></div>';
$( '#form-errors' ).html( errorsHtml );
}
});
});
Server Side - PHP (laravel):
Route:
Route::post('profile/image','ProfileFormsController#postImage');
Controller
public function postImage(Request $request){
if($request->ajax()){
$imagedestination = 'images\profile';
$file = $request->file('image');
$image_name = time()."-".$file->getClientOriginalName();
$file->move($imagedestination, $image_name);
}
}
You forgot about CSRF validation.
As far as this request is POST, you have to send also CSRF token, or, at least, disable check for this route.
Go to app/Http/Middleware/VerifyCsrfToken.php
and add your POST-url to protected $except = [];
Also read this part of documentation: https://laravel.com/docs/master/routing#csrf-protection