dptgpyl61857413 2016-03-21 09:36
浏览 221
已采纳

laravel - 在路由中使用一个GET参数,但将其余参数传递给视图中的url

I have a route like this:

Route::get('/s', 'DashboardController@search');

A super simple function to open a view like this:

public function search() {
    return view('search');
}

And a form like this:

<form action="/s" method="get">
    <input type="text" name="location" placeholder="Where" required>
    <input type="text" id="checkin" placeholder="Check in" class="datepicker" required>
    <input type="text" id="checkout" placeholder="Check out" class="datepicker" required>
    <input type="submit" value="search now">
</form>

At the moment it sends the parameters through happily with the url like so:

http://localhost/s?location=Location%2C+Name%2C+Here&checkin=21-03-2016&checkout=22-03-2016

However, what I would really, really like to see in my URL is the following:

http://localhost/s/Location_Name_Here?checkin=21-03-2016&checkout=22-03-2016

Now I'm aware that I will have to modify the location myself to get it to read n a pretty manner like that. But how would I make it so that I end up using the location which is a parameter in the form, as a route, whilst still successfully passing the check in and check out parameters to the page in the traditional GET= fashion? I really want to keep that. I know I can do away with it entirely and just manipulate on the back end, but I'd rather not.

Thanks!

  • 写回答

1条回答 默认 最新

  • douwo5710 2016-03-21 10:14
    关注

    Submitting my own solution to help others. Not sure if there's a way to do it via laravel. So instead I created an event listener for the form itself, a click (not a submit listener or it'll loop).

    Gave my submit button and form an ID:

    <form action="/s" method="get">
        <input type="text" name="location" placeholder="Where" required>
        <input type="text" id="checkin" placeholder="Check in" class="datepicker" required>
        <input type="text" id="checkout" placeholder="Check out" class="datepicker" required>
        <input type="submit" id="submit-button" value="search now">
    </form>
    

    Then the listener:

    $('#submit-button').click(function(e){
        e.preventDefault();
        var location = $('[name="location"]').val();
        //Console log to see it, and re-write it as necessary
        console.log(location);
        //Action attribute is changed to suit, and then it submits properly
        $('#form-id').attr('action', "/s/" + location).submit();
    });
    

    Now it works :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?