doubaran2438 2018-04-24 12:03
浏览 104

根据Laravel中的表单值搜索数据

I have a search form where you can select different cities and a house-type. Can anyone tell me how I can put the entered form data into the URL and display the filtered data with the GET method in Laravel?

Thank you.

  • 写回答

2条回答 默认 最新

  • doukuibi9631 2018-04-24 12:36
    关注
    <?php 
      $selectedcities=[]; 
        if (\Illuminate\Support\Facades\Input::has('city')) {
        foreach (\Illuminate\Support\Facades\Input::get('city') as $c) {
          $selectedcities[] = $c;
        }
      }
    ?>
    

    <script>
    
    $(document).ready(function() {
      $('.refine-your-search input[type=checkbox]').click(function() {
        var prm = $(this).closest('div').attr('id');
        searchByCheckbox(prm);
      });
    });
    
    function searchByCheckbox(prm) {
      var searchString = '';
      var searchString = $('#'+prm+'String').val();
      if (searchString != '') {
        searchString = searchString + '&';
      }
      var tmpQString = '';
      $('#'+prm+' input[type=checkbox]:checked').each(function() {
        if(tmpQString != ''){
          tmpQString = tmpQString + '&';
        }
        
        tmpQString = tmpQString + prm + '[]=' + $(this).val();
      });
    
      // alert(tmpQString);
      searchString = searchString + tmpQString;
      var searchUrl = baseUrl + '?' + searchString;
      window.location.replace(searchUrl);
      return window.location.href = searchUrl;
    }
    
    </script>
    <div id='city'>
      <!-- use foreach for all cities -->
      <input type="checkbox" value="Cityname" name="city[{{ $city->id }}]" {{ (in_array($city->id,  $selectedcities)) ? 'checked="checked"' : '' }}>
      <label>{{$city->name}}</label>
      <input type="hidden" id="cityString" name="cityString" value="<?php echo http_build_query(Request::except(['city']));?>">
      <!-- end foreach -->
    </div>

    <!-- In Controller you can get -->
    <?php 
      $city = $request->city; //and use it in your query LIKE
    ?>
    
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致