类似 Google 自动建议脚本中的 xmlHttpRequest 问题
我正在尝试建立一个类似于 Google Suggestion (或 Autosuggestion?) 的自动建议搜索字段。
我使用纯 javaScript/AJAX 和 2 个文件:index.php 和 ajax-submit.php(是我实际查询数据库的文件)。但目前我只是回显文本以进行调试。
有几个问题:
问题 1: 问题是 firebug 输出:当我在搜索输入中键入某些内容时,xmlhttp 就没有定义 [已解决,见下文]。
问题2:我还想回显搜索输入的内容,如下所示:
echo $_GET['search_text'];
或者
if(isset($_GET['search_text'])) {
echo $search_text = $_GET['search_text'];
}
但我收到以下错误:*未定义的索引:ajax-submit.php中的search_text*
所以这是我的函数建议调用:
<form action="" name="search" id="search">
<input type="text" name="search_text" id="search_text" onkeydown="suggest();" />
</form>
<div id="results" style="background:yellow"></div>
这是我的函数 suggest():
<script type="text/javascript">
//function does not needs params because is unique to the input search_text
function suggest() {
//browser object check
if(window.xmlHttpRequest) {
xmlhttp = new xmlHttpRequest();
}
else if (window.ActiveXObject) {
//console.log("error");
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
//when the onreadystatechange event occurs
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByID('results').innerHTML = xmlhttp.responseText;
}
}//end onready
xmlhttp.open('GET', 'ajax-submit.php', true);
xmlhttp.send();
}//end suggest
</script>
这是我的 php ajax-submit 文件:
<?php
echo 'Something';
?>
有人可以帮我调试吗?这可能是一个范围问题,但我不知道。
第二个问题是您通常如何在 Firebug 中调试 Ajax 请求?
谢谢
I am trying to build up an autosuggestion search field similar to Google Suggestion (or Autosuggestion?).
I am using pure javaScript/AJAX and 2 files: index.php and ajax-submit.php (is the file where I will actually query the database). But for moment I am simply echo a text for debugging.
There are a few issues:
Issue 1: The issue is the firebug outputs: xmlhttp is not defined as soon as I type something in the search input [solved, see below].
Issue2: I would like also to echo the content of the search input something like this:
echo $_GET['search_text'];
or
if(isset($_GET['search_text'])) {
echo $search_text = $_GET['search_text'];
}
but I get the following error: *Undefined index: search_text in ajax-submit.php*
So here is my function suggest call:
<form action="" name="search" id="search">
<input type="text" name="search_text" id="search_text" onkeydown="suggest();" />
</form>
<div id="results" style="background:yellow"></div>
And here is my function suggest():
<script type="text/javascript">
//function does not needs params because is unique to the input search_text
function suggest() {
//browser object check
if(window.xmlHttpRequest) {
xmlhttp = new xmlHttpRequest();
}
else if (window.ActiveXObject) {
//console.log("error");
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
//when the onreadystatechange event occurs
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByID('results').innerHTML = xmlhttp.responseText;
}
}//end onready
xmlhttp.open('GET', 'ajax-submit.php', true);
xmlhttp.send();
}//end suggest
</script>
and here is my php ajax-submit file:
<?php
echo 'Something';
?>
Can someone help me debug? It might be a scope issue but I have no clue.
The second question would be how would you normally debug an Ajax request in Firebug?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,它
并不是
要创建真正的跨浏览器兼容的 XHR 对象,请执行以下操作:
Actually, it is
not
To have a true cross-browser compliant XHR object creation, go with this:
用途:
不
Use:
not
我写了一个更好的实现:跨浏览器/更可读的代码,函数拆分。下面是代码。不幸的是,很难读取 php echo 文本,它不会读取变量 search_text,我不知道为什么:
和 HTML 代码:
和 ajax-submit.php:
I wrote a better implementation: cross-browser/more readable code, function splits. Below is the code. Unfortunately tough reads php echo text it won't read the variable search_text, I don't know why:
and HTML code:
and ajax-submit.php: