我正在用html进行实时搜索。
我工作得很好,但我遇到了一个小问题,这是我的索引代码:
<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代码:
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();
}我得到的结果是正确的,但当我清空完整的输入框时,我将从数据库中获得完整的列表框,在清空输入框时,我想清除列表框
发布于 2013-01-11 16:22:55
你应该在PHP和用户端保护你的代码。为此,请检查用户确实发送了多少封信:if(s.length < 2 ) return;以防止AJAX请求
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上也要保护它:
if(count($_REQUEST['qsearch']) < 2) return false;https://stackoverflow.com/questions/14274034
复制相似问题