php – Laravel Redirect based on radio button selection-ThrowExceptions

Exception or error:

How can I redirect the user based on the selected radio button?

This is a multi-page form using sessions. The data gets stored with the last page of the form. I am following this tutorial: https://www.5balloons.info/multi-page-step-form-in-laravel-with-validation/

I want it to do this: If checked ‘yes’, go to the next page if checked ‘no’, skip the next page and go to the one after.

<form action="/form/update-step4" method="post">
      @csrf
      <div class="form-group">
      <label class="radio-inline"><input type="radio" name="is_employed" value="1" {{{ (isset($user->is_employed) && $user->is_employed == '1') ? "checked" : "" }}}> Yes</label>

       <label class="radio-inline"><input type="radio" name="is_employed" value="0" {{{ (isset($user->is_employed) && $user->is_employed == '0') ? "checked" : "" }}} checked> No</label>
       </div>
       @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
            </ul>
        </div>
       @endif

<button type="submit" class="btn btn-primary">Continue</button>
</form>
How to solve:

You can simply use some switch or if/else in controller actions to define redirect url based on selected checkboxes — something like this:

public function postCreateStep2(Request $request)
{
    /**-------some logic------**/
    if ($request->some_checkbox_1 === 1) {
        $route = '/products/create-step3';
    } else if ($request->some_checkbox_2 == 1 && $request->some_checkbox_3 != 1) {
        $route = '/products/create-step4';
        if ($request->some_checkbox_5 == 1) {
            $route = '/products/create-step5';
        }
    }
    /** 
     * etc... 
     * same with route or GET params
     **/
    return redirect($route);

}

Answer´╝Ü

Try using:

return redirect('products/create-step2');

or

redirect()->route('product.createStep2');

before you should defined in web.php name this route

Leave a Reply

Your email address will not be published. Required fields are marked *