[英]How to pass form data as route parameters in Laravel 5.2
我想只使用一個表單的一個字段的值,其中兩個字段作為控制器的路由參數。 到目前為止我所獲得的只是附加到url的一堆查詢字符串參數。
我的表格:
{{ Form::open(['route' => ['anuncio.especificar_tipo_imovel', $valorCep = 'valorCEp'], 'method' => 'GET']) }}
<input type="hidden" value="14405024" id="valorCep" name="valorCep"/>
<label for="tbCep"/>
<input autocomplete="off" id="tbCep" style="width:400px;" name="cep" type="text" />
</label>
<input type="submit" value="continuar">
{{ Form::close() }}
我有這樣的路線:
Route::get('anuncio/especificar_tipo_imovel/{valorCep}', [
'as' => 'anuncio.especificar_tipo_imovel',
'uses' => 'AnuncioController@especificar_tipo_imovel'
]);
和這樣的動作方法
public function especificar_tipo_imovel(Request $request, $valorCep)
{
return view('especificar_tipo_imovel');
}
我想要發送的值是隱藏字段的值:valorCep我想要一個像http://my_route/34834839
這樣的URL,隱藏字段的值和$valorCep
路由參數。
我的網址是這樣的:
http://my_route/valorCEp?valorCep=14405024&cep=Rua++jardim+pedreiras14405024
請注意,您在視圖中使用文字“valorCep”分配$valorCep
。 你應該從你的控制器傳遞它。
public function especificar_tipo_imovel(Request $request, $valorCep)
{
return view('especificar_tipo_imovel', ['valorCep' => $valorCep]);
}
在你看來:
{{ Form::open(['route' => ['anuncio.especificar_tipo_imovel', $valorCep], 'method' => 'GET']) }}
你可以用它
public function especificar_tipo_imovel(Request $request, $cep)
{
$valorCep = $request->valorCep;
return view('especificar_tipo_imovel', ['valorCep' => $valorCep]);
}
您不能將隱藏輸入的值傳遞給路徑,如$valorCep = 'valorCEp'
,這樣您只需傳遞字符串'valorCEp'作為參數。 檢查你的url,是參數是字符串的路由,加上輸入的值(導致GET方法)。
除非你在變量中有valorCep輸入的值,並且傳遞這個變量而不是'valorCEp'字符串,否則你需要一些javascript。 像這樣的東西:
腳本(使用jQuery)
$('input[type=submit]').on('click', function(event){
event.preventDefault();
var valorCep = $('#valorCep').val();
$('form').attr('action', 'anuncio/especificar_tipo_imovel/'+valorCep);
$('form').submit();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.