【问题标题】:hide div from div when there is no search result没有搜索结果时从 div 中隐藏 div
【发布时间】:2018-04-03 16:56:02
【问题描述】:

我已经从 stackoverflow 和其他网站尝试了很多方法,但每当我成功隐藏 div 时。根本不显示搜索结果。

我已经尝试了 :empty 选择器并摆弄了 php 代码和 js 代码。但由于我对此很陌生,我似乎无法破解错误。我做错了什么?

我的 HTML

            <div class='search'>
            <form class="searchbox" action='index.php' method='post' autocomplete='off'>
                <input type='text' name='search' class='searchform' placeholder='Søg efter skole...' onkeyup="searchq();">
            </form>
            <div id="output"></div>
        </div>

PHP

    <?php

include("connection.php");
$output = '';
//collect

if(isset($_POST['searchVal'])) {
    $searchq = $_POST['searchVal'];
    $searchq = preg_replace("#[^a-zA-Z0-9æøå]#i"," ", $searchq);

    $query = mysqli_query($mysqli, "SELECT * FROM `products` WHERE name LIKE '%$searchq%'") or die("could not search");
    $count = mysqli_num_rows($query);

     if($_POST['searchVal'] == NULL) {
        $output = '';
    } else {

        while($row = mysqli_fetch_array($query)) {            
            $name = $row['name'];
            $id = $row['id'];
            $output .= '<a href="kantine.php?id='.$id.'" class="searchres">'.$name.'</a><br>';
        }  
    }
}

echo "<div class='output'>$output</div>";

?>

还有 JS

                function searchq() {
                var searchTxt = $("input[name='search']").val();
                $.post("search.php", {
                    searchVal: searchTxt
                }, function(output) {
                    $("#output").html(output);
                });
            }

【问题讨论】:

标签: javascript php html css mysql


【解决方案1】:

HTML

<div class='search'>
        <form class="searchbox" action='index.php' method='post' autocomplete='off'>
            <input type='text' name='search' class='searchform' placeholder='Søg efter skole...' onkeyup="searchq();">
        </form>
        <div id="output" style="display: none;"></div>
</div>

PHP

.....
echo $output;

JS

function searchq() {
    var searchTxt = $("input[name='search']").val();
    $.post("search.php", {
        searchVal: searchTxt
    }, function(output) {
        $("#output").html(output);
        if(output.length > 0) {
            $("#output").show();
        }
    });
}

【讨论】:

    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多