Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当用户未在搜索框中输入任何内容时,不执行任何操作_Php_Search Engine - Fatal编程技术网

Php 当用户未在搜索框中输入任何内容时,不执行任何操作

Php 当用户未在搜索框中输入任何内容时,不执行任何操作,php,search-engine,Php,Search Engine,当用户未在搜索框中输入任何内容时,如何使页面不执行任何操作 当搜索框中没有任何内容时,我希望页面保持不变 我该怎么做 这是我当前的代码: <html> <head> <title> Brandon's Search Engine </title> </head> <script type="text/javascript"> /

当用户未在搜索框中输入任何内容时,如何使页面不执行任何操作

当搜索框中没有任何内容时,我希望页面保持不变

我该怎么做

这是我当前的代码:

<html>
    <head>
        <title>
            Brandon's Search Engine
        </title>
    </head>
    <script type="text/javascript">

        //document.getElementById("suggestion")

        function getSuggestion(q) {
            var ajax;
            if(window.XMLHttpRequest)//for ie7+, FF, Chrome
                ajax = new XMLHttpRequest();//ajax object
            else
                ajax = new ActiveXObject("Microsoft.XMLHTTP");//for ie6 and previous
            ajax.onreadystatechange = function() {
                if(ajax.status === 200 && ajax.readyState === 4) {
                    //if result are not there then don't display them
                    if(ajax.responseText === "")
                        document.getElementById("suggestion").style.visibility = "hidden";
                    else {
                        document.getElementById("suggestion").style.visibility = "visible";
                        document.getElementById("suggestion").innerHTML = ajax.responseText;
                    }
                }
            };
            ajax.open("GET", "suggestion.php?q=" + q, false);
            ajax.send();
        }
    </script>
    <body>
        <form method="GET" action="search.php" name="q">
            <table align="center">
                <tr>
                    <td>
                        <h1><center>Brandon's Search Engine</center></h1>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <input type="text" name="q" style="height: 27px; width: 650px; padding: 2px"
                               onkeyup="getSuggestion(this.value)" autocomplete="off" onblur="blur() document.getElementById('suggestion').style.visibility = 'hidden'"/>

                        <div id="suggestion" style="width: 648px">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <input type="submit" name="submit" value="Search" style="height: auto; width: 60px; padding: 2px" />
                        <input type="reset" value="Clear" style="height: auto; width: 50px; padding: 2px" />
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        To insert your site in result fill in the form at <a href="insert.php">here</a>.
                    </td>
                </tr>
            </table>
            <input type="hidden" name="page" value="0" />
        </form>
    </body>
</html>

您应该验证您的表单,如:

<form method="GET" action="search.php" name="q" onsubmit="return validateForm()">

您可以在表单提交功能上执行此操作,如:

您的表单如下所示:

<form method="GET" action="search.php" name="q" id="searchForm">
$("#searchForm").submit(function(e) {
if($("#q").val() == '')
{
    e.preventDefault();
}
});

顺便说一下,此解决方案使用JQuery。

您只需向表单元素添加一个id属性,并向搜索输入字段添加一个id属性。然后将此脚本添加到页面中的任何脚本标记中。
$("#searchForm").submit(function(e) {
if($("#q").val() == '')
{
    e.preventDefault();
}
});