Laravel 中的搜索表单
Search form in Laravel
我有一个带有两个输入字段的表单,它应该路由到 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() !!}
你应该有类似于下面的代码。在信任来自客户端的任何内容之前,您显然需要验证您的输入。您也可以轻松地在视图中添加验证。路由文件可能是 routes.php,具体取决于您的 laravel.
版本
文件: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>
我有一个带有两个输入字段的表单,它应该路由到 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() !!}
你应该有类似于下面的代码。在信任来自客户端的任何内容之前,您显然需要验证您的输入。您也可以轻松地在视图中添加验证。路由文件可能是 routes.php,具体取决于您的 laravel.
版本文件: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>