Php 如果搜索框中没有键入任何内容,使用jQuery的即时搜索将提供所有字段

Php 如果搜索框中没有键入任何内容,使用jQuery的即时搜索将提供所有字段,php,jquery,Php,Jquery,这是我主要搜索部分的PHP代码。 此搜索工作正常,但有一种情况除外:当搜索框中没有键入任何内容时,它将返回表中的所有值。 我怎样才能解决这个问题 <?php mysql_connect("localhost","root","0392") or die("Could not connect"); mysql_select_db("emm") or die("could not connect"); $output=""; if(isset($_POST['s

这是我主要搜索部分的PHP代码。 此搜索工作正常,但有一种情况除外:当搜索框中没有键入任何内容时,它将返回表中的所有值。 我怎样才能解决这个问题

<?php
    mysql_connect("localhost","root","0392") or die("Could not connect");
    mysql_select_db("emm") or die("could not connect");
    $output="";
    if(isset($_POST['searchVal'])){
      $searchq = $_POST['searchVal'];
      $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

      $query=mysql_query("select name, code from employee_table where name LIKE     '%$searchq%' or code LIKE '%$searchq%'");
      $count = mysql_num_rows($query);

      if($count == 0){
        $output='No Matching Results Found !!';
      }
      else{
        while($row = mysql_fetch_array($query)){            
          $name = $row['name'];
          $code= $row['code'];

          $output.="<a href='altprofile.php?cod=".$code."'><div class='col-md-3'><input type='text' name='nam' disabled='disabled' value='".$name."'></input></div></a> ";
        }
      }
    }
    echo($output);
?>
试试这个

 ...
 if(isset($_POST['searchVal']) && $_POST['searchVal'] != ''){
   $searchq = $_POST['searchVal'];
   $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

   $query=mysql_query("select name, code from employee_table where name LIKE     '%$searchq%' or code LIKE '%$searchq%'");
   $count = mysql_num_rows($query);

   if($count == 0){
     $output='No Matching Results Found !!';
   }
   else{
     while($row = mysql_fetch_array($query)){
       $name = $row['name'];
       $code= $row['code'];

       $output.="<a href='altprofile.php?cod=".$code."'><div class='col-md-`enter code here`3'>`enter code here`<input type='text' name='nam' `enter code here`disabled='disabled' value='".$name."'></input></div></a> ";
     }
  }
}
...
注意:不要使用mysql使用mysqli或PDO

if(isset($_POST['searchVal']) && !empty($_POST['searchVal'])){

// DO code

}else{
  echo "no results";
}

只要做以下两件事,如果条件允许,就改变

//checks value is not empty
if(isset($_POST['searchVal']) && !empty($_POST['searchVal']))
{
  //all your stuff
}
else
{
echo "No results"
}

我没有显示jquery部分。请告知是否需要。I thin k您需要添加一个if check条件。。如果$searchq==则不进行进一步的步骤我尝试了相同的条件,但不起作用……我不知道您是如何做到的,但它非常简单,应该可以起作用