PHP MySQL AJAX中的多个搜索选项

时间:2015-12-13 06:23:46

标签: php jquery mysql ajax

我想通过AJAX方法在PHP文件中添加多个搜索选项。通过一次搜索(按名称搜索)选项,它的工作正常,但是当我放置另一个搜索字段(如按性别搜索)时,其搜索仅限于性别。我无法同时搜索这两个。 这是主要的index.php文件

 This is tablepage.php file where ajax method calling + display data

<script charset="UTF-8">

    function pagination(page)
      {
          var search = $("input#fieldSearch").val();
          var record = $("select#pageRecord").val();
          var gender = $("select#fieldSearch1").val(); 
          if (search!=="")
          {
            dataString = 'starting='+page+'&gender='+ gender+'&name='+search+'&perpage='+ record+'&random='+Math.random();
        }

         else{
              dataString = 'starting='+page+'&perpage='+record+'&random='+Math.random();
            }

        $.ajax({
         url:"tablepage.php",
         data: dataString,
         type:"GET",
         success:function(data)
         {
            $('#divPageData').html(data);
          }
       });
     }

        function loadData()
       {
          var dataString;
          var search = $("input#fieldSearch").val();
          var record = $("select#pageRecord").val();
          var gender = $("select#fieldSearch1").val();
        dataString = '&gender=' + gender + 'name='+ search + '&perpage=' + record;


       $.ajax({
             url: "tablepage.php",
             type: "GET",
             data: dataString,
            success:function(data)
              {
                 $('#divPageData').html(data);
              }
        });
       }
    </script>

<?php
   error_reporting(E_ALL^E_NOTICE);
   include('pagination_class.php');
   include('connect.php');

    if (isset($_GET['name']) and !empty($_GET['name'])){
       $name = $_GET['name'];
        $sql = "select * from fmaf where name like '%$name%'";

          }
           if (isset($_GET['gender']) and !empty($_GET['gender'])){
              $gender = $_GET['gender'];
            $sql = "select * from fmaf where gender = '$gender'";

           }
          else{

        $sql="select * from fmaf left join status ON fmaf.id = status.sid GROUP BY fmaf.id ";

        }

          if(isset($_GET['starting'])){ //starting page
                 $starting=$_GET['starting'];
                }else{
              $starting=0;
             }

           $recpage=$_GET['perpage'];

            $obj = new pagination_class($sql,$starting,$recpage);       
           $result = $obj->result;
       ?>  

这是pagination.js文件

$(document).ready(function(){

            $('#divLoading').ajaxStart(function(){
            $(this).fadeIn();
            $(this).html("<img src='loading.gif' /> ");
              }).ajaxStop(function(){
                $(this).fadeOut();
            });


           loadData();


          function loadData()
           {
             var dataString;
             var search = $("input#fieldSearch").val();
             var record = $("select#pageRecord").val();
             var gender = $("select#fieldSearch1").val();   


           dataString = '&gender=' + gender + 'name='+ search + '&perpage=' + record;


           $.ajax({
               url: "tablepage.php",
                 type: "GET",
             data: dataString,
               success:function(data)
               {
                $('#divPageData').html(data);
              }
        });
       }

          $("form#formSearch").submit(function()
            {
                 loadData();
               return false;
              });


                 }); 

其余的pagination.class.php文件工作正常。

1 个答案:

答案 0 :(得分:0)

我只是检查单个GET参数,如果用户想要查找名称和性别vise,那么我只需添加此代码

if( !empty( $_GET['name']) && !empty( $_GET['gender']))