简体   繁体   English

Laravel中的Ajax搜索功能-页面加载

[英]ajax search function in laravel -page load

I am new to Laravel. 我是Laravel的新手。 I am using ajax for a search function. 我正在使用ajax进行搜索。 The scripts and function work well but when it displays the search result it reload the whole page and displays not with div. 脚本和函数运行良好,但是在显示搜索结果时会重新加载整个页面,并且不显示div。 It reloads the whole page. 它将重新加载整个页面。

I have given only the table id to display but it loads the whole page. 我只给出了要显示的表ID,但它会加载整个页面。 Why is this happening? 为什么会这样呢? where is the mistake done? 错误在哪里做? Can anyone find it and give the solution to this problem. 谁能找到它并提供解决此问题的方法。

My page loads like this : 我的页面加载如下:

<script>
  $(document).ready(function(){
  $("button").on('click',function(e){
  e.preventDefault();
   var str=  $("#search").val();
   if(str == "") {
  $( "#tabledata" ).html( data );   
  } else {
  $.get( "{{ url('create?id=') }}"+str, 
  function( data ) {
   $( "#tabledata" ).html( data );   
   });
    }
     });  
      }); 
    </script>

Search function: 搜索功能:

<div class="panel">
  <br>
    <div>
      <h2 style="font-family:Garamond;color:black;font-weight:bold;margin-left:5%; font-size:40px;"> 
    </div>
    <div class="col-lg-12" >
      <form class="navbar-form navbar-right" method="get">
        <div class="form-group">
          <input type="text"  class="form-control" id="search" placeholder="Search Here.." value="{{ request('search') }}">
        </div>
        <button type="submit" id="search" class="btn btn-default">Submit</button>
      </form>
    </div>

    <div class="container"  style=" max-width: 1150px;" >
      <table class="table table-bordered" style=" margin-top: 60px; font-family:timesnewroman">
        <thead class="text-justify">
        <tbody class="table-striped" id="tabledata">
          <tr class="table-info" style="font-weight:bold;font-size:20px;">
            <th class="text-justify"><strong> Name </strong></th>
            <th class="text-justify"><strong> Email</strong></th>
            <th class="text-justify"><strong> PhoneNumber</strong></th>
            <th class="text-justify"><strong> Action </strong></th>
          </tr>
          @foreach($users as $user)
          <tr class="table-info" style="font-weight: bold ;font-size:15;font-family:timesnewroman;background-color:#f9f9f9">
            <td>{{$user->name}}</td>
            <td>{{$user->email}}</td>
            <td>{{$user->phno}}</td>
            <td>
              <a href="{{ route('show',$user->id) }}">
                <span class="glyphicon glyphicon-eye-open" style=" color:#00a0df"></span>
              </a> &nbsp;
              <a href="{{ route('edit',$user->id)}}">
                <span class="glyphicon glyphicon-pencil" style=" color:green">
              </a>&nbsp;
              <a href="{{ route('delete',$user->id) }}"  onclick="return confirm('Are you sure to delete?')">
                <span class="glyphicon glyphicon-trash" style=" color:red"></span>
              </a>
            </td>
          </form>  
        </tr>
        @endforeach
      </tbody>
      </thead>
    </table>

Controller search function: 控制器搜索功能:

public function search(Request $request)
{
  $search = $request->id;
  if (is_null($search)) {
    return view('admin.datas.create');        
  } else {   
    $users = data::where('name','LIKE',"%{$search}%")
                   ->get();
    return view('admin.datas.create',compact('users'));
        // return view('demos.livesearchajax')->withPosts($posts);
  }
}

OK. 好。 so problem is in you jquery 所以问题出在你的jQuery

Solution 1 : 解决方案1:

First add id to your form : 首先将id添加到您的表单中:

<form id="search-form" class="navbar-form navbar-right" method="get"> 

       // id is added on above line

      <div class="form-group">
       <input type="text"  class="form-control" id="search" placeholder="Search Here.." 
             value="{{ request('search') }}">
     </div>
     <button type="submit" id="search" class="btn btn-default">Submit</button>
  </form>

and for jquery use : 对于jquery使用:

<script>
    $(document).ready(function(){
    $("#search-form").on('submit',function(e){  // Use form submit event
      e.preventDefault();
      var str=  $("#search").val();
      if(str == "") {
         $( "#tabledata" ).html( data );   
       } else {
          $.get( "{{ url('create?id=') }}"+str, 
          function( data ) {
             $( "#tabledata" ).html( data );   
         });
        }
     });  
  }); 
</script>

Solution 2 : 解决方案2:

Change your button type from submit to button so, that it doesn't submit the form 将按钮类型从“ submit更改为“ button以便不提交表单

<form class="navbar-form navbar-right" method="get">
 <div class="form-group">
   <input type="text"  class="form-control" id="search" placeholder="Search Here.." 
         value="{{ request('search') }}">
 </div>
 <button type="button" id="search" class="btn btn-default">Submit</button>

 // type changed on above line

And don't use click event on button element it will bind to all buttons on current page use "#search" id to bind click event 并且不要在button元素上使用点击事件,它会绑定到当前页面上的所有按钮,并使用“ #search” ID来绑定点击事件

<script>
$(document).ready(function(){
   $("#search").on('click',function(e){  // Use button id to bind event

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM