网络请求未完全正常工作

发布于 2024-11-19 15:19:44 字数 1954 浏览 0 评论 0原文

我有一个小问题。我想要进行自动搜索,但是我的网络请求(自动)的结果与我直接按 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-11-26 15:19:44

一个更优雅的解决方案,而不是使用本机 XMLHttpRequests 和所有其他解决方法是 jQuery.ajax。另外,其他事项:

  • 输入元素没有结束

适用于 jsFiddle 的修改示例:http://jsfiddle.net/e9drS/

HTML:

<form id="form">
    <input type="text" id="search" name="search" value="Naam..." />

    <br />

    <input type="text" id="search2" name="search2" value="Plaats..." />

    <input type="submit" value="zoeken" />
</form>

JavaScript:

$(function() {

$('#search, #search2').focus(function() { // clear on focus
    $(this).val('');
}).keyup(function() { // perform request on keyup
    $.ajax({ type: 'GET',
             url:  'klanttabel.php',
             data: { search:  $('#search').val(),
                     search2: $('#search2').val() },
             success: function(text) {
                 $("#txtHint").html(text);
             }
           });
});

});

A more elegant solution instead of using native XMLHttpRequests and all other workarounds is jQuery.ajax. Also, other things:

  • Input elements don't have a closing </input>.

A modified example that works on jsFiddle: http://jsfiddle.net/e9drS/.

HTML:

<form id="form">
    <input type="text" id="search" name="search" value="Naam..." />

    <br />

    <input type="text" id="search2" name="search2" value="Plaats..." />

    <input type="submit" value="zoeken" />
</form>

JavaScript:

$(function() {

$('#search, #search2').focus(function() { // clear on focus
    $(this).val('');
}).keyup(function() { // perform request on keyup
    $.ajax({ type: 'GET',
             url:  'klanttabel.php',
             data: { search:  $('#search').val(),
                     search2: $('#search2').val() },
             success: function(text) {
                 $("#txtHint").html(text);
             }
           });
});

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文