Laravel 5 On POST Status 302 Found

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
View

<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

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

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

add this to your header and combine it with what your sending
Also NOTE your doing a $.get :

<meta name="csrf" value="{{ csrf_token() }">

I believe one of the things Form::open() does is include csrf token.

eldewiny, please post the form html.

I add csrf token to my header stil give me the 302 and in my form i have csrf token as will, Stil i can’t find the problem where ???

Post the form html output.

After submit no result just the page refreshed but if we will look in firebug here is the 302 not Fount

Post the router definitions for /post-ads.

Routes

        Route::get('/ajax-subcategory', 'formsController@getAjax');
	Route::get('post-ads', 'formsController@createPostAds');
	Route::post('post-ads', 'formsController@storePostAds');

Controller

public function createPostAds()
    {
        $category = Category::all();
    	return view('front.froms.post-ads', compact('category'));
    }

    public function getAjax()
    {
        $cat = Input::get('cat_id');
        $sub = Subcategory::where('category_id', '=', $cat)->get();
        return Response::json($sub);
    }

i change the category and subcategory fields to match the controller requrest … Now after submit i redirect to home page and the data saved … But no image and still give me the same 302 Found

Where is the method storePostAds defined in the controller? What is the code in that method.

i posted up already but i change in this function from category_id to category and subcategory_id to subcategory like the form fields

Replace it with this code, post the form again, and what is the response.

public function storePostAds(Request $request) {
  echo 'Hello';
}

true, form open does add it to the field but I normally add one extra and combine the ajax form input with the meta csrf.

A bit weird I know but thats how my script work.
Do not think I don’t care about how it should work please :slight_smile:

Thank you guys and here is the working code

    public function createPostAds()
    {
        $category = Category::all();
    	return view('front.froms.post-ads', compact('category'));
    }

    public function getAjax()
    {
        $cat = Input::get('cat_id');
        $sub = Subcategory::where('category_id', '=', $cat)->get();
        return Response::json($sub);
    }

    public function storePostAds(Request $request)
    {
        $this->validate($request, [
           'title' => 'required',
           'description' => 'required',
           'image' => 'required',
           'category' => 'required',
           'subcategory' => 'required',
        ]);
        //$data = $request->all();
        $email = $request['email'];
        $name = $request['name'];
        $title = $request['title'];
        $description = $request['description'];
        $category = $request['category'];
        $subcategory = $request['subcategory'];
        $image = $request->file('image');
        $user = User::where('email', $email)->first();
        if(!$user){
            $user = new User();
            $user->email = $email;
            $user->name = $name;
            $user->save();
        }
        if($image->isValid()){
            $name = $image->getClientOriginalName();
            $destination_path = 'images/'.$name;
            Storage::disk('s3')->put($destination_path, 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('/');

    }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.