Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mohamedhamidi's avatar

Redirect URL based on selected options on form submit

Can i update the URL with the selected options in the form, ex: localhost/Usa/California/Gardena , without using javascript ?

my route is like that :

    Route::get('/{country}/{region}/{city}', 'User\UserLocationsController@store')->name('country.store');

My form is like :

    <form action="" method="GET">
        <select class="form-control" name="country_id" id="country_id">
                    <option value="0" disable="true" selected="true">  Select Country </option>
                </select>
        <select class="form-control" name="region_id" id="region_id">
                    <option value="0" disable="true" selected="true">  Select Region </option>
                </select>
        <select class="form-control" name="districts" id="districts">
                    <option value="0" disable="true" selected="true">  Select City </option>
                </select>
    </form>
0 likes
7 replies
cmdobueno's avatar

What does your store function look like?

Additionally...

You would need JS for this... at least to do it that way...

Other Option

Route:

Route::get('/store','User\UserLocationsController@store')->name('country.store');

Form

<form action="{!! route('country.store')" method='GET'>
    {{ csrf_field() }} //Cant remember if this is correct... but double check
    <select class="form-control" name="country_id" id="country_id">
        <option value="0" disable="true" selected="true">  Select Country </option>
    </select>
        <select class="form-control" name="region_id" id="region_id">
        <option value="0" disable="true" selected="true">  Select Region </option>
    </select>
        <select class="form-control" name="districts" id="districts">
        <option value="0" disable="true" selected="true">  Select City </option>
    </select>
    <button type='submit'>Submit</button>
</form>

Controller

public function store( Request $request){
    //First Note: You could use a custom request for validation (its a good idea)

    //Do storage stuff
}

Otherwise, you would have to be more clear as to why you want to dynamically route based on user input... anything is possible, but the question is whether it is practical...

Snapey's avatar
Snapey
Best Answer
Level 122

If you don't want to use javascript then post the form to the server and then redirect to the route requested.

So if your form action was 'resolver' and form method post

web.php

Route::post('/resolver','ResolverController@resolve');
Route::get('/{country}/{region}/{city}', 'User\UserLocationsController@store')->name('country.store');

Then create a ResolverController


public function resolve(Request $request)
{
    return redirect(route('country.store',$request->only('country','region','city')));
}
mohamedhamidi's avatar

@cmdobueno

Hello , thank you so much for quick answer sir , my work consist on prompting the user about their location and save it in a session (store function ) so all the items they search for are in the location selected from that form and basically

        public function store()

     {

             session()->put($country->name,$region->name,$city->name);

              return redirect()->route('welcome.index', [$country, $region, $city]);


     }
Snapey's avatar

there you go then. Just POST to the store route.


<form action="{!! route('country.store')" method='POST'>
{{ csrf_field() }}

        public function store(Request $request)

     {

             session()->put($request->only('country','region','city'));

              return redirect()->route('welcome.index',$request->only('country','region','city'));


     }

but you need to align everything because at the moment your fields are called country_id, region_id and districts

mohamedhamidi's avatar

@Snapey

Thank you a lot for your answer , but it's still giving me : Missing required parameters for [Route: country.store] [URI: {country}/{region}/{city}], Error

The location data that's passed to the view are from this controller :

CountryController :

    class CountryController extends Controller

      {

       public function countries(Request $request){
          $countries = Country::all();

          return view('welcome', compact('countries'));
      }

       public function regions(){
       $countries_id = Input::get('country_id');
     $regencies = Region::where('country_id', '=', $countries_id)->get();
      return response()->json($regencies);
     }

     public function cities(){
          $regions_id = Input::get('region_id');
          $districts = City::where('region_id', '=', $regions_id)->get();
         return response()->json($districts);
      }


    }
Snapey's avatar

As I say, you need to make sure you use the right names everywhere.

Your form has different name fields to what you want on your welcome page.

Please or to participate in this conversation.