A_ID
A_ID

Reputation: 11

Insert a Form value from a select in a dynamic url

While using laravel to create a movie catalog, I am trying to extract the value from an HTML Form and insert it in the URL.

The objective is that, from the main page which is:
http://127.0.0.1:8000/index/

I want it to extract an ID value from the form and insert it in the url:

http://127.0.0.1:8000/index/1

http://127.0.0.1:8000/index/2

http://127.0.0.1:8000/index/3

As each one is a dynamic view that will display each movie information from the database. The form is already recognizing the ids and is displaying them in the select form in page, but I am not able to have that value used to insert it in the url as shown above.

Please help me, here is my code:

index.blade.php

                <form action=" WHAT TO PLACE HERE??? " method="POST">
                    @csrf

                    <select name="selector">
                        <option value="" disabled selected> --- ID --- </option>
                        @foreach($movies as $movie)
                        <option value="{{ $movie->id }}">{{ $movie->id }}</option>
                        @endforeach

                    </select>

                    <button>Buscar</button>
                </form>

web.php

Route::get('/index', 'App\Http\Controllers\MovieController@index');
Route::get('/index/create','App\Http\Controllers\MovieController@create'); 
Route::post('/index','App\Http\Controllers\MovieController@store'); 
Route::get('/index/{id}','App\Http\Controllers\MovieController@show');
Route::delete('/index/{id}','App\Http\Controllers\MovieController@destroy'); 

MovieController.php

class MovieController extends Controller
{
    public function index() {
        $movies = Movie::all();    
        return view('movies.index', ['movies' => $movies,]);
    }

    public function show($id) {
        $movie = Movie::findOrFail($id); 
        return view('movies.show', ['movie' => $movie]);
        }


    public function create() {
    return view('movies.create');
    }

    public function store(){
    
        $movie = new Movie(); 

        $movie->title = request('title'); 
        $movie->synopsis = request('synopsis'); 
        $movie->year = request('year'); 
        $movie->cover = request('cover');      

        $movie->save();    

        return redirect('/')->with('mssg','La película a sido registrada'); 
    }

    public function destroy($id) {
        $movie = Movie::findOrFail($id);
        $movie->delete(); 

        return redirect('/index/'); 
    }

}

Upvotes: 1

Views: 558

Answers (1)

Ali
Ali

Reputation: 300

Replace you form as:

index.blade.php

 <form action="" method="POST" id="form_id">
    @csrf
        <select name="selector" id="selector">
          <option value="" disabled selected> --- ID --- </option>
          @foreach($movies as $movie)
            <option value="{{ $movie->id }}">{{ $movie->id }}</option>
          @endforeach
       </select>
    <button type="submit">Buscar</button>
 </form>

Add script after this :

<script>
$('#selector').change(function(){
    var selected_value = $(this).val();
   $('#form_id').attr('action', 'http://127.0.0.1:8000/'+selected_value);
});
</script>

This will set your action dynamic as per selection with your select tag

Upvotes: 0

Related Questions