在点击时使用ajax进行搜索,并在同一页面上显示结果 [英] Search with ajax on click and show results in same page laravel

查看:116
本文介绍了在点击时使用ajax进行搜索,并在同一页面上显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个包含搜索表单的视图:

I've setup a view which contains a search form:

<form>
<input type="hidden" id="product_id" value="{{$tour->id}}">
<div class="form-group col-md-4">
    <label>Date:</label>
    <div class="input-group date">
        <div class="input-group-addon">
            <i class="fa fa-calendar"></i>
        </div>
        <input type="text" class="form-control pull-right" id="start"  placeholder="Start Date">
    </div>
</div>
<div class="form-group col-md-4">
    <label>End:</label>
    <div class="input-group date">
        <div class="input-group-addon">
            <i class="fa fa-calendar"></i>
        </div>
        <input type="text" class="form-control pull-right"  id="end" placeholder="End Date">
    </div>
</div>
<div class="form-group col-md-4" id="dateprice-search">
    <label></label>
    <button type="submit" class="btn" id="btn-search" >
        Search
    </button>
</div>

下面是处理表单请求的ajax代码:

The below is the ajax code to handle the request of the form:

$(document).ready(function() {
$('#dateprice-search').on('click', '#btn-search', function() {
    $.ajax({
        type: 'post',
        url: '/date-price',
        data: {
            '_token': $('input[name=_token]').val(),
            'product_id': $("#product_id").val(),
            'start': $("#start").val(),
            'end': $("#end").val()
        },
        success: function(data) {
            $('.shadow-z-1').show();
            $('.shadow-z-1').append("<tr class='liquid-row><td>" + data.start + "</td><td>"+ data.end + "</td><td>" + data.end + "</td><td><a class='btn-m btn btn-m-success'>Available</a></td></tr>");

        }
    });
  });
});

路线:

Route::post('/date-price','GetPublicController@datePrice')
->name('searchDate');

最后是控制器中的方法以给出结果:

And finally method in controller to give the results:

    public function datePrice(Request $request){
    $start = $request->start;
    $end = $request->end;

    $dates = DatePrice::where('tour_id','=',$request->product_id)
    ->whereBetween('start', array($request->start, $request->end))
    ->whereBetween('end', array($request->start, $request->end))
    ->get();
    return response()->json($dates);
}

首先,在提交表单http://localhost:8000/trips/popular/trekking/test之前,URL看起来像这样,单击搜索按钮后,URL变成http://localhost:8000/trips/popular/trekking/test?. inspect元素的控制台部分在脚本中未显示任何错误.我在这里犯了什么错误?

At first the url looks like this before submitting the form http://localhost:8000/trips/popular/trekking/test and url becomes http://localhost:8000/trips/popular/trekking/test? after clicking the search button. Console section of inspect element shows no error in script. What mistake I had made over here ?

推荐答案

这表示由于type="submit"

1)更改为type="button"

 <button type="button" class="btn" id="btn-search" >

2)在这里click event应该用于button而不是div,因此更改selector并在jquery中添加e.preventDefault();可以防止默认提交.

2) Here click event should be for button not to div so change the selector and add e.preventDefault(); in jquery to prevent the default submit .

$('#btn-search').on('click', '#btn-search', function() { e.preventDefault(); });

注释:

第一名:您的action attribute丢失,因此表单将提交同一页面.

1st : your action attribute missing so form will be submit same page .

第二:您的method attribute丢失,因此将采用默认的GET方法

2nd : your method attribute missing so it will take default GET method

这篇关于在点击时使用ajax进行搜索,并在同一页面上显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆