2

I have a modal view (the one from bootstrap) in the front end.

Upon clicking the submit button the user will be going to a function in controller:

Route::post('post_question', array('uses' => 'QuestionController@postQuestion'));

And at the end of the postQuestion i want to redirect to another page.

I tried:

return Redirect::to('mywebsite/question/1');
return Response::make( '', 302 )->header( 'Location', 'mywebsite/question/1' );
return Redirect::route('question/'.$question_id)->with("question",Question::find($question_id));
header("Location: mywebsite/question/$question_id");

none seem to work though.

The thing is, i can see the request in XHR but just that the page is not redirected. Is the modal somehow blocking the behavior?

Cœur
  • 37,241
  • 25
  • 195
  • 267
TanC
  • 153
  • 3
  • 13

2 Answers2

1

You can redirect with an AJAX request. However, you will find that the results will not be quite what you expected.

On a redirect, Laravel will should set your response code header as a redirect response and then the content of the redirected page would be sent.

You could do one of two things depending on how you wanted to handle things.

  1. Send a JSON response back to the submitted form with a meta data parameter and then use this meta data in your success function to set window.location.

Your Laravel controller responding to the post would look a bit like this:

public function postQuestion()
{
    // DO stuff to set your $question

    return [
        'question' => $question,
        'meta' => [
            'redirect_url' => url('mywebsite/question/'.$question->id),
            'status' => '400',
            // Any other meta data you may want to send
        ],
    ];
}

Then assuming you are doing some jQuery AJAX call, change your success callback (I'm calling it questionSubmitSuccess here):

questionSubmitSuccess = function (data) {
    // Anything you may want to do before redirecting the user

    if (data.meta.redirect_url) {
        // This redirects the page
        window.location = data.meta.redirect_url;
    }
}
  1. Continue redirecting from your controller and then do something a bit more similar to rails turbo links and replace the entire page with Javascript:

You can do this a few ways: using [Modify the URL without reloading the page browser History API), or using jQuery.load to submit your form.

The browser history API might work a bit easier as it would still allow you to handle response errors, but it only works in more modern browsers. jQuery.load would likely require rewriting a bit of your AJAX submitting code and is harder to handle things like errors (it will replace your page content no matter the status code from what I can tell), but it has better browser support.


IMO, the first approach is a bit more maningful as the API endpoint is usable by something other than this single implementation. Also, there are fewer points of failure and error states to manage compared to trying to replace your entire DOM without a page reload.

Community
  • 1
  • 1
Ryan Tablada
  • 440
  • 4
  • 11
0

U can put button inside form and when u submit that button pass data from page to controller and from controller call the another page with that data like return View::make('users.index,compact('data'));

priyaa
  • 47
  • 1
  • 9