首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >清除在html中按下键的列表

清除在html中按下键的列表
EN

Stack Overflow用户
提问于 2013-01-11 16:05:31
回答 1查看 181关注 0票数 0

我正在用html进行实时搜索。

我工作得很好,但我遇到了一个小问题,这是我的索引代码:

代码语言:javascript
复制
<form id="quick-search" action="livesearch.php" method="GET" >
<p>
Search:
<input id="qsearch" type="text" name="qsearch" onkeyup="liveSearch()" />
<input type="submit" />
</p>
<div id="searchResults">

</div>
</form>

下面是我的js代码:

代码语言:javascript
复制
function liveSearch()
{
    var url = "livesearch.php";
    var s = document.getElementById('qsearch').value;
    http.open("POST", "livesearch.php?qsearch="+s, true);
    http.onreadystatechange = function() 
    {
        if(http.readyState == 4 && http.status == 200) 
        {
            document.getElementById('searchResults').innerHTML = 'Suggestions are as follow'+http.responseText; 
            //alert(http.responseText);
        } 
    }

    http.send();
}

我得到的结果是正确的,但当我清空完整的输入框时,我将从数据库中获得完整的列表框,在清空输入框时,我想清除列表框

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-11 16:22:55

你应该在PHP和用户端保护你的代码。为此,请检查用户确实发送了多少封信:if(s.length < 2 ) return;以防止AJAX请求

代码语言:javascript
复制
function liveSearch()
    {
    var url = "livesearch.php";
    var s = document.getElementById('qsearch').value;
    if(s.length < 2) return; // here You escape if there isn't enough letters to search
    http.open("POST", "livesearch.php?qsearch="+s, true);
    http.onreadystatechange = function() 
    {
        if(http.readyState == 4 && http.status == 200) 
        {
            document.getElementById('searchResults').innerHTML = 'Suggestions are as follow'+http.responseText; 
            //alert(http.responseText);
        } 
    }

    http.send();
}

但请记住,在PHP上也要保护它:

代码语言:javascript
复制
if(count($_REQUEST['qsearch']) < 2) return false;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14274034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档