【问题标题】:Insert a Form value from a select in a dynamic url从动态 url 中的选择中插入表单值
【发布时间】:2021-09-28 13:30:54
【问题描述】:

在使用 laravel 创建电影目录时,我试图从 HTML 表单中提取值并将其插入 URL。

目标是,从主页是:
http://127.0.0.1:8000/index/

我希望它从表单中提取一个 ID 值并将其插入 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

因为每一个都是一个动态视图,它将显示数据库中的每部电影信息。 表单已经在识别 id 并在页面中的选择表单中显示它们,但我无法将该值用于将其插入到 url 中,如上所示。

请帮帮我,这是我的代码:

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/'); 
    }

}

【问题讨论】:

  • @a-id 在选择值更改时将 id 添加到 url 的目的是什么?意思是,你想重定向到选定的电影吗?
  • 是的,我有一个动态视图调用“show”,当添加带有每个电影 id 的主 url 时,它会创建一个动态视图,显示所选 id 电影信息。该部分有效,例如,如果我在 url 上手动添加 id,它会显示电影,但是,我想通过 html 表单中的选择来做到这一点。
  • @a-id 你可以使用html的list-view来满足你的需求,是不是一定要用form?!

标签: laravel


【解决方案1】:

将您的表格替换为:

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>

在此之后添加脚本:

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

这将根据您的选择标签的选择设置您的动作动态

【讨论】:

  • 感谢您的帮助和您的时间,不幸的是,它没有工作,似乎 Laravel 妨碍了一条错误消息:“SQLSTATE [23000]:完整性约束违规:1048 Column 'title ' 不能为空(SQL:插入moviestitlesynopsisyearcover)值(?,?,?,?))“。
  • 请首先检查您的参数名称,如您在 store 方法中插入 title, synopsis, year, cover 并在 blade 中传递 selector,然后永远不会工作。您必须使用要保存的参数,否则使该字段 可为空 或将其与 隐藏 值一起传递。
猜你喜欢
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
相关资源
最近更新 更多