javascript - 带有单选按钮过滤的 PHP + MYSQL 即时搜索

标签 javascript php jquery mysql forms

我在本指南的帮助下得到了 https://www.youtube.com/watch?v=_AqM9U3mi9A使用 PHP 和 MYSQL 创建了一个显示即时搜索结果(无需按提交按钮)的工作搜索表单。

然后我想根据按下的单选按钮过滤搜索结果。现在我也可以使用它(部分是在本指南 https://www.youtube.com/watch?v=DVS4qoB98U8 的帮助下),但在我的搜索表单上按下提交时。由于某种原因,它不适用于即时搜索结果,这是我的问题。

index.php(表单):

<form class="form-custom" role="search" action="index.php" method="POST">
   <div class="form-group">
      <label for="all" class="radio-btn">
         <input id="all" class="radio-custom" type="radio" name="searchfilter" value="all" checked="checked"> ALL
      </label>
      <label for="sports" class="radio-btn">
         <input id="sports" class="radio-custom" type="radio" name="searchfilter" value="sports"> SPORTS
      </label>
      <label for="e-sports" class="radio-btn">
         <input id="e-sports" class="radio-custom" type="radio" name="searchfilter" value="e-sports"> E-SPORTS
      </label>
      <label for="show-business" class="radio-btn">
         <input id="show-business" class="radio-custom" type="radio" name="searchfilter" value="show-business"> SHOW BUSINESS
      </label>
   </div>
      <div class="form-group">
      <input type="text" name="search" autocomplete="off" class="form-control form-control-custom" placeholder="Search..." onkeyup="searchq();">
      <button type="submit" name="submit" value="" class="btn btn-default btn-form-custom">Submit</button>
      </div>
</form>

<div class="test" id="output">

<!-- this is where instant search results are supposed to appear -->

</div>

index.php(jquery - 需要即时搜索结果才能工作):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function searchq(){
var searchTxt = $("input[name='search']").val();

$.post("search.php",{searchVal: searchTxt}, function(output){
        $("#output").html(output);

});
}
</script>

search.php(PHP代码):

<?php

include_once("connect.php");

$output = '';


if (isset($_POST['searchVal']) && isset($_POST['searchfilter']) && trim($_POST['searchVal']) != '' && strlen('searchVal') > 3 ){

$searchq = $_POST['searchVal'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

if($_POST['searchfilter'] == "all") {

    $sqlCommand = "(SELECT  * FROM sports WHERE Title LIKE '%$searchq%') UNION (SELECT  * FROM e_sports WHERE Title LIKE '%$searchq%') UNION (SELECT  * FROM show_business WHERE Title LIKE '%$searchq%')";

} else if($_POST['searchfilter'] == "sports") {

        $sqlCommand = "SELECT * FROM sports WHERE Title LIKE '%$searchq%'";

} else if($_POST['searchfilter'] == "e-sports") {

        $sqlCommand = "SELECT  * FROM e_sports WHERE Title LIKE '%$searchq%'";

} else if($_POST['searchfilter'] == "show-business") {

        $sqlCommand = "SELECT  * FROM show_business WHERE Title LIKE '%$searchq%'";

} 

$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);

if($count == 0){
     $output .= '<p class="p-nof">No results found</p>';
    }else{
    $output .= '<ul ="dropdown">';
    $output .= '<p>Search results: '.$count.'</p>';
        while($row = mysql_fetch_array($query)){
        $title = $row['Title'];
        $url = $row['url'];
        $id = $row['id'];

            $output .= '<a class="searchresult" href="'.$url.'"><li> '.$title.'</li></a>';
       }
    $output .= '</ul>';
   }
}
echo($output); 

?>

在此先感谢您的帮助!

编辑:

我将 javascript 更改为以下内容:

<script type="text/javascript">
function searchq(){
var searchTxt = $("input[name='search']").val();
var searchFilter = $("input[name='searchfilter']").val();

$.post("search.php",{searchVal: searchTxt, searchfilterVal: searchFilter}, function(output){
    $("#output").html(output);

});
}
</script>

通过此更改,即时搜索结果可以像以前一样工作,但单选按钮过滤不起作用。似乎它只使用来自第一个 radio 输入的数据而忽略其余的。当我单击其他单选按钮时,它会继续使用表单中第一个列出的数据。它不会随着我的点击而改变。

我仍然需要帮助!提前致谢!

最佳答案

调整您的 JS 以发布 searchFilter 的值

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function searchq(){
var searchTxt = $("input[name='search']").val();
var searchFilter = $("input[name='searchfilter']").val();

$.post("search.php",{searchVal: searchTxt, searchFilter: searchfilter}, function(output){
        $("#output").html(output);

});
}
</script>

关于javascript - 带有单选按钮过滤的 PHP + MYSQL 即时搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531367/

相关文章:

PHP + Node - 将参数传递给已经运行的进程

带有 execCommand 调用的 jquery 颜色选择器

javascript - NodeJS如何加速这个数组创建功能

javascript - jQuery:选择父列表项

php - Laravel 4 - 生产站点自定义错误页面

php - 使用自定义 PHP MVC 在另一个 foreach 循环中进行 foreach 循环

jQuery如何通过多个循环从json获取数据

JavaScript `defer` 似乎不起作用

php - 直接通过 PHP 包含 JavaScript,而不是通过脚本标记从最终页面加载它

javascript - 如何在具有对象的 React 应用程序中循环遍历 javascript 数组并获取具有特定值的属性的计数