【问题标题】:Pass form data through url by setting in route通过在路由中设置通过 url 传递表单数据
【发布时间】:2019-01-10 19:07:43
【问题描述】:

我已经在我的 Laravel 项目中设置了一条路线,如果它使用 href 我会这样做 {{route('destination'),$country_from,$country_to}} 但现在我想将表单数据作为 $country_from$country_to 传递请如何我这样做

<form action="{{route('destination')}}">
            @csrf
                        <input type="text"
                            class="search-form flexdatalist"
                            name="origin"
                            placeholder="country_from"
                            data-search-in='name'
                            data-min-length='1'
                            />


                        <img src="{{URL::asset('img/location.svg')}}" class="search-icon origin" width="28px" height="21px"/>
                    </div>
                    <div class=" col-md-3 mx-0 px-0">
                           <input type="text"
                                class="search-form flexdatalist"
                                name="country_to"
                                data-search-in='name'
                                data-min-length='1'
                                placeholder="Travelling to"

                            />
                            <img src="{{URL::asset('img/location.svg')}}" class="search-icon destination" width="28px" height="21px"/>
                    </div>
                    <div class="col-md-3 mx-0 px-0">
                    <input type="text"
                                class="search-form flexdatalist"
                                name="residence"
                                data-search-in='name'
                                data-min-length='1'
                                placeholder="Residing In"

                            />
                            <img src="{{URL::asset('img/location.svg')}}" class="search-icon residence" width="28px" height="21px"/>
                    </div>
                    <div class="col-md-3 px-0">
                        <input type="submit" value="Search Visas" class="btn btn-primary search-btn"/>
                        </form>

路线

Route::get('/{country_from}/{country_to}',  'DestinationCountriesController@index')->name('destination');

【问题讨论】:

  • 如果这是您想要的路线,您将需要 javascript 来获取用户输入和链接的值。
  • 或者.. 将表单提交到单独的路由,然后使用提交的参数重定向到目标路由

标签: php laravel laravel-5 laravel-routing


【解决方案1】:

我不明白你到底想做什么:

您想在表单提交后将用户重定向到 URL,并在重定向 URL 中使用表单值?

  • 在您的表单 POST 控制器中,从 $request 检索输入并返回 return redirect()-&gt;route('route_name', ['country_from' =&gt; $request-&gt;country_from, 'country_to' =&gt; $request-&gt;country_to])

您想使用 URL 值作为输入值,所以在页面加载时,输入会填充 URL 值?

  • 您的“目标”路由控制器方法接受参数$country_from$country_to,因为您在路由中声明了这些变量。您的控制器中有这些变量,您可以清理它们,将它们绑定到返回的视图(例如:return view('view', $data)return view('view', compact('country_from', 'country_to'))),然后像往常一样在刀片中访问它。
  • 您还可以使用\Request::route('country_from') 访问URL 值。在将其用作输入之前,您应该清理这些值。
  • 使用刀片中的这些变量作为输入value 属性,或placeholder,在任何地方{{$country_to}}

【讨论】:

  • 它重定向并显示此网址 http://localhost:8000/destination?_token=xKY7QEzceYP6zdYrtnNriOgbBbs9izeBeE6GyDQl&amp;origin=Nigeria&amp;flexdatalist-origin=Nigeria&amp;country_to=Australia&amp;residence=United+Arab+Emirates 而不是 /Nigeria/United+Arab+Emirates
  • 您想要实现的目标是什么?第一还是第二?
  • 第二个,我正在努力实现第二个
  • 如果是第二个,我不知道重定向发生在哪里。第二个是关于在刀片中使用 URL 值作为输入值。
  • 这是根据你的回答public function reroute(Request $request) { $country_from = $request-&gt;country_from; $country_to = $request-&gt;country_to; redirect()-&gt;route('/{country_from}/{country_to}', ['country_from' =&gt; $country_from, 'country_to' =&gt; $country_to]); }的代码
猜你喜欢
  • 2015-03-20
  • 2017-04-23
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2021-11-12
  • 2011-11-03
相关资源
最近更新 更多