如何在Laravel 5.2中将表单数据作为路由参数传递

问题描述 投票:3回答:3

我想只使用一个表单的一个字段的值,其中两个字段作为控制器的路由参数。到目前为止我所获得的只是附加到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这样的网址,隐藏字段的值和$valorCep路由参数。

我的网址是这样的:

http://my_route/valorCEp?valorCep=14405024&cep=Rua++jardim+pedreiras14405024
php laravel model-view-controller
3个回答
2
投票

请注意,您在视图中使用文字“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']) }}

0
投票

你可以用它

public function especificar_tipo_imovel(Request $request, $cep)
{
    $valorCep = $request->valorCep;

    return view('especificar_tipo_imovel', ['valorCep' => $valorCep]);
}

0
投票

您不能将隐藏输入的值传递给路径,如$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();
});
© www.soinside.com 2019 - 2024. All rights reserved.