I'm trying to create new post useing laravel , ajax and s3 , But every time i try submit the form i get Status Code:302 Found , I Hope really some help me
Firebug
Here in firebug result image
META
<meta name="csrf" value="{{ csrf_token() }}">
VIEW
The form view with csrf token
<div class="col-md-8 col-md-offset-2">
{!! Form::open(array(
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true
)) !!}
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
<label for="cats">Select Category list :</label>
<select class="form-control" id="category" name="category">
<option value="">Select Category</option>
@foreach($category as $cat)
<option value="{{$cat->id}}">{{$cat->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="cats">Select Subcategory list :</label>
<select class="form-control" id="subcategory" name="subcategory">
<option value=>Select Subcategory</option>
<option value=""></option>
</select>
</div>
<div class="form-group">
{!! Form::label('image', 'Upload Image') !!}
{!! Form::file('image', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Description: ') !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Your Email: ') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Post Free Ad', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
</div>
CONTROLLER
First valid the requist and than create new email for the user if he did't have and than save the post with the user
public function storePostAds(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'image' => 'required',
'category_id' => 'required',
'subcategory_id' => 'required',
]);
$email = $request['email'];
$title = $request['title'];
$description = $request['description'];
$category = $request['category_id'];
$subcategory = $request['subcategory_id'];
$image = $request->file('image');
$user = User::where('email', $email)->first();
if(!$user){
$user = new User();
$user->email = $email;
$user->save();
}
if($image->isValid()){
$name = $image->getClientOriginalName();
$key = 'images/'.$name;
Storage::disk('s3')->put($key, file_get_contents($image));
}
$post = new Post();
$post->title = $title;
$post->description = $description;
$post->category_id = $category;
$post->subcategory_id = $subcategory;
$post->image = $image;
$user->posts()->save($post);
return redirect('/');
}
Ajax
ajax to get subcategory foreach category after select
(function($){
$('#category').on('change', function(e){
var cat_id = e.target.value;
$.get('/ajax-subcategory?cat_id=' + cat_id, function(data){
var subcategory = $('#subcategory');
subcategory.empty();
$.each(data, function(index, subcatObj){
subcategory.append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');
});
});
});
}(jQuery));
The name of your category and subcategory fields are "category" and "subcategory" but are being referred to as "category_id" and "subcategory_id" respectively in your Controller code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With