【问题标题】:Search form in LaravelLaravel 中的搜索表单
【发布时间】:2017-12-01 01:00:34
【问题描述】:

我有一个带有两个输入字段的表单,它应该以两个输入字段作为参数(服务和城市)路由到 search.servicecity。我怎样才能说,通过单击提交按钮,输入字段被用作参数?

{!! Form::open(['route' => ['search.servicecity',service,city]]) !!}
   <div class="row">
     <div class='col-md-5'>
       <input type="text" class='form-control form-control-lg' name="service" id="service" placeholder="activity" data-action="{{ route('search.autocompleteservice') }}"/>
        <div id='searchresultservice' style='text-align:left'></div>
      </div>
      <div class='col-md-5'>
        <input type="text" class='form-control form-control-lg' name="city" id="city" placeholder="city or zip" data-action="{{ route('search.autocompletecity') }}"/>
        <div id='searchresultcity' style='text-align:left'></div>
      </div>
      {{ Form::submit('Suchen', array('class'=>'btn btn-success btn-lg btn-block col-md-2'))}}
    </div>

{!! Form::close() !!}

【问题讨论】:

  • 提交表单后,输入将成为 $request 的一部分。您可以在控制器中处理它。如果您需要操作表单操作,您可以通过重载点击提交方法来实现。
  • 所以你的意思是,我应该调用一条路线,例如search.search,不需要参数但证明$request数据,然后使用请求中的参数路由到search.servicecity?
  • 我创建了一个名为 getSearchRequest 的方法,该方法现在应该处理路由,但它说 Action path\SearchController@getSearchRequest 未定义。我用这个动作来称呼它:{!! Form::open(array('action' => "SearchController@getSearchRequest")) !!}

标签: php laravel-5.5


【解决方案1】:

您应该有类似于下面的代码。在信任来自客户端的任何内容之前,您显然需要验证您的输入。您也可以轻松地在视图中添加验证。根据你的 laravel 版本,路由文件可能是 routes.php。

文件:routes\web.php

Route::get('search/servicecity', 'SearchController@index')->name('searchServiceCityForm');
Route::post('search/servicecity', 'SearchController@process')->name('processServiceCity');

文件:app\Http\Controller\SearchController.php

class SearchController extends Controller
{
  public function index()
  {    
      return view('search.ServiceCity');
  }
  public function process(Request $request)
  {
      $service = $request->input('service');
      $city = $request->input('city');

      /* Do something with data */

      return view(search.result, compact('service','city'));
  }

文件:resources\views\search\ServiceCity.blade.php

<html><head><title>Search for City and Service</title></head><body>
<form method="post" action="{{url('search/servicecity')}}">
 {{csrf_field()}}
 <div>
  <label for="Service">Service:</label>
  <input type="text" name="service">
 </div>
 <div>
  <label for="city">City:</label>
  <input type="text" name="city">
 </div>
</body></html>

文件:resources\views\search\Result.blade.php

<html><head><title>Result of Service City Search</title></head><body>
<div><span>Searched for service: {{ $service }}</span></div>
<div><span>Searched for city: {{ $city }}</span></div>
</body></html>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多