网络请求未完全正常工作
我有一个小问题。我想要进行自动搜索,但是我的网络请求(自动)的结果与我直接按 Enter 键的结果之间存在差异。
我希望结果完全相同,结果应该是我按回车键时得到的结果。
代码请求:
function showUser(str, str2)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "klanttabel.php?search=" + encodeURIComponent(str) + "&search2=" + encodeURIComponent(str), true);
xmlhttp.send();
}
代码查询(给我正确的结果)
if($search != ''){
$where[] = "( klant_id LIKE '%" .$search."%' OR voornaam LIKE '%" .$search."%' OR achternaam LIKE '%" .$search."%' OR email LIKE '%" .$search."%' OR plaats LIKE '%" .$search."%' OR bedrijfsnaam LIKE '%" .$search."%' ) ";
if($search2 != ''){
$where[] = " ( klant_id LIKE '%" .$search2."%' OR voornaam LIKE '%" .$search2."%' OR achternaam LIKE '%" .$search2."%' OR email LIKE '%" .$search2."%' OR plaats LIKE '%" .$search2."%' OR bedrijfsnaam LIKE '%" .$search2."%') ";
}
$query_where = (is_array($where))?" WHERE ".implode(" AND ", $where):"";
$result = mysql_query("SELECT * FROM klant ".$query_where." ORDER BY klant_id DESC");
$i = 0;
}
我有两个输入字段,以便您可以搜索两个不同的值。当我输入它们并按回车键时,它会给出相应的结果。
但如果我让它自动执行,它只会给出我输入的最后一个字段的结果。
所以基本上,它只是搜索最后一个字段。我认为这与我的输入字段有关,所以这里是代码:
<form method="get">
<input onfocus="this.value=''" type="text" name="search" value="Naam..." onkeyup="showUser(this.value)"></input><br />
<input onfocus="this.value=''" type="text" name="search2" value="Plaats..." onkeyup="showUser(this.value)"></input><input type="submit" value="zoeken" name="submit2" </input></form>
I have a little problem. I want to do automatic searching, but there is a difference between the results from my webrequest (automatic) and the results if I just hit enter.
I want to results to be the exactly the same, the result should be the one I get when I hit enter.
Code request:
function showUser(str, str2)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "klanttabel.php?search=" + encodeURIComponent(str) + "&search2=" + encodeURIComponent(str), true);
xmlhttp.send();
}
Code query (wich gives me the right results)
if($search != ''){
$where[] = "( klant_id LIKE '%" .$search."%' OR voornaam LIKE '%" .$search."%' OR achternaam LIKE '%" .$search."%' OR email LIKE '%" .$search."%' OR plaats LIKE '%" .$search."%' OR bedrijfsnaam LIKE '%" .$search."%' ) ";
if($search2 != ''){
$where[] = " ( klant_id LIKE '%" .$search2."%' OR voornaam LIKE '%" .$search2."%' OR achternaam LIKE '%" .$search2."%' OR email LIKE '%" .$search2."%' OR plaats LIKE '%" .$search2."%' OR bedrijfsnaam LIKE '%" .$search2."%') ";
}
$query_where = (is_array($where))?" WHERE ".implode(" AND ", $where):"";
$result = mysql_query("SELECT * FROM klant ".$query_where." ORDER BY klant_id DESC");
$i = 0;
}
I have two input fields, so that you can search for two different values. When I type them in and hit enter, it gives me the corresponding result.
But if I let it do it automatically, it just gives the result from the last field I typed in to.
So basically, it just searches for the last field. I think it has something to do with my input fields, so here is the code for that:
<form method="get">
<input onfocus="this.value=''" type="text" name="search" value="Naam..." onkeyup="showUser(this.value)"></input><br />
<input onfocus="this.value=''" type="text" name="search2" value="Plaats..." onkeyup="showUser(this.value)"></input><input type="submit" value="zoeken" name="submit2" </input></form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个更优雅的解决方案,而不是使用本机
XMLHttpRequests
和所有其他解决方法是jQuery.ajax
。另外,其他事项:。
适用于 jsFiddle 的修改示例:http://jsfiddle.net/e9drS/。
HTML:
JavaScript:
A more elegant solution instead of using native
XMLHttpRequests
and all other workarounds isjQuery.ajax
. Also, other things:</input>
.A modified example that works on jsFiddle: http://jsfiddle.net/e9drS/.
HTML:
JavaScript: