为什么不将搜索功能与选择功能一起使用?使用PHP,jQuery,Ajax

尤拉

我想在网络上使用选择功能和搜索功能。并希望在网络上显示人们的DB数据表列。

如果我在$ sql中使用'or',则可以使用它的搜索功能。如果我在$ sql中使用'and',则可以使用select函数。

但是我想同时使用这两个功能。我的代码有什么问题?如何获得两种功能?

这是“ load_data_select.php”脚本代码。

<script>
$(document).ready(function(){
  function load_data(query)
  {
    $.ajax({
      url:"load_data.php",
      method:"POST",
      data:{query:query},
      dataType:"text",
      success:function(data)
      {
        $('#show_product').html(data);
      }
  });
  }
  $('#search_text').keyup(function(){
    var search = $(this).val();
    if(search != '')
    {
      load_data(search);
    }
    else {
    load_data(search);
    }
  });
   $('#Category').change(function(){
        var Category = $(this).val();
        $.ajax({
             url:"load_data.php",
             method:"POST",
             data:{Category:Category},
             success:function(data)
             {
               $('#show_product').html(data);
             }
          });
     });
     $('#Obtain').change(function(){
        var Obtain = $(this).val();
        $.ajax({
             url:"load_data.php",
             method:"POST",
             data:{Obtain:Obtain},
             success:function(data)
             {
               $('#show_product').html(data);
             }
        });
     });

   });
</script>

这是load_data.php。如果将“或类别”更改为“与类别”,则可以选择功能。但无法使用搜索功能。

$sql = "SELECT * FROM 190406test2
 WHERE Name LIKE '%".$_POST["query"]."%'
 or Category = '".$_POST["Category"]."'
 or Obtained_From = '".$_POST["Obtain"]."'
 ";
 $result = mysqli_query($conn, $sql);
 if(isset($_POST["query"]) or
 isset($_POST["Category"]) or isset($_POST["Obtain"])
 )
 {
   if($_POST["query"] != ''
     or $_POST["Category"] != '' or $_POST["Obtain"] != '')
      {
           $sql;
      }
      else
      {
           $sql = "SELECT * FROM 190406test2
           ";
      }

我希望可以使用工作选择功能和搜索功能,但实际输出仅是选择功能或搜索功能。

西尔万

1)要同时使用这两个功能,您需要一个新按钮:

使用ajax时,实际上不需要表单标签。您可以通过以下方式将输入字段打包在div-inputscontainer中:

<div id="inputs_container">

<input type="text" id="inp1"></input>
<input type="text" id="inp2"></input>
<input type="text" id="inp3"></input>
<button  id="submit_btn">Save</button>

</div>

<script>
    $(document).ready(function() {

   $("#submit_btn").click(function() {

var Obj_allinputs = new Object();

$("#inputs_container input[type=text]").each(function() {
        Obj_allinputs[this.id]=this.value;
    });

$.ajax({
         url:"load_data.php",
         method:"POST",
         data:Obj_allinputs,
         success:function(data)
         {
           $('#show_product').html(data);
         }

}</script>

2)在load_data.php中:

$sql='SELECT * FROM 190406test2';
if(count($_POST)>0)  $sql.=' WHERE '

//if(isset($_POST["search"])) sql.=" Name LIKE '%?%' OR ";

$ar_bindparam=array();
foreach ($_POST as $key=>$val) 
{
    $ar_bindparam[$key] = $_POST[$val];
    if($key=="search") sql.=" $key LIKE '%?%' OR ";
     else sql.=" $key = '?' OR ";
}
$sql = preg_replace('#(\sOR\s)$#', '', $sql);

//$result = mysqli_query($conn, $sql);  //weak regarding injection

 //$db     = new mysqli("localhost","root","","tests");
 if(  $stmt = $db->prepare($sql) )
 {  //stmt is of type mysqli_stmt
    if(count($ar_bindparam)>0)
    {
    $ar_bindparam = array_merge(array(str_repeat('s', count($ar_bindparam))), array_values($ar_bindparam));
    $ref    = new ReflectionClass('mysqli_stmt');
    $method = $ref->getMethod("bind_param");
    $method->invokeArgs($stmt,$ar_bindparam); 
      //FOR php version < 5.3 : call_user_func_array(array(&$stmt, 'bind_param'), $ar_bindparam);
    }
$smt->execute();  

  //echo $result in the right format back to your ajax call
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么 jquery isVisible 不能与滚动功能一起使用

为什么循环功能不能与空列表一起使用?

为什么功能序列不能与ggplot2一起使用?

为什么我的计数器不能与“ alternate”功能一起使用?

为什么我的共享功能只能与whatsapp一起使用?

为什么此代码不能与反应功能一起使用?

为什么odd.fst 不能与过滤器功能一起使用?

为什么JQuery不能与CryptoJS一起使用?

为什么childElementCount不能与jQuery一起使用?

为什么VBA不能在Word和Excel中将替换功能与CRLF一起使用

为什么 Switch-Case 中的 View 不能与 react-native 中的功能组件一起使用

如何将搜索功能与if函数一起使用

如何使功能与Firestore一起使用?

将轮询功能与缓冲流一起使用

胖箭头功能不能与jQuery getJSON一起使用?

为什么我的 :nth-child() 选择器不能与 ::before 一起使用

为什么<c:forEach>在JSP Spring中不能与Ajax请求一起使用?

为什么 foreach 不能与 String 一起使用?

为什么“ COALESCE”不能与“ GROUP BY”一起使用?

LibreOffice为什么不能与HUD一起使用?

为什么javascript“ this”不能与“ each”一起使用?

rand为什么不能与AbstractFloat一起使用?

为什么 grep 不能与 iostat 一起使用?

setuid为什么不能与mount一起使用?

为什么 CUDA 不能与 easyocr 一起使用?

为什么不将向量和列表一起使用?

为什么不将反引号与for循环一起使用

jQuery不能与ajax一起使用?

从脚本到PHP解释器的参数传递不能与引号一起使用,为什么?