【发布时间】:2012-04-09 04:04:21
【问题描述】:
我有一个简单的投票系统来处理 2 个 php 文件。但是,它不适用于博客(prolly 因为现在 1 个文件是 HTML)
更具体地说,投票结果在我的数据库中注册。它只是无法输出响应。
这是我的代码:
<div id="poll" style="width:200px;overflow:hidden;text-align:center;">
Do you like this poll?
<div style="text-align:left;width:180px;margin:0 auto;">
<input type="radio" name="poll" id="poll1" checked>Yes, it`s great
<input type="radio" name="poll" id="poll2">Yes...
<input type="radio" name="poll" id="poll3">Not bad...
<input type="radio" name="poll" id="poll4">No!
</div>
<input type="button" value="Vote!" onClick="vote();"/>
</div>
<script type="text/javascript">
function vote(){
for(var i=1;i<=4;i++){
if(document.getElementById('poll' + i).checked){
//Check which one has been checked
var sendto = 'http://myhostingadd.com/vote.php?vote=' + i;
}
}
// Call the vote.php file
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest;
xmlhttp.open("GET",sendto,false);
xmlhttp.send(null);
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",sendto,false);
xmlhttp.send();
}
//Output the response
document.getElementById('poll').innerHTML = xmlhttp.responseText;
}
</script>
【问题讨论】:
-
看在上帝的份上,请使用 jQuery! ActiveXObject?!!!
-
我该怎么做?对不起,我从教程中得到了这个
-
检查jQuery。 Goggling "jQuery tutorials" 会得到很多好的结果。 jQuery 将使用选择器和内置函数让您的生活更轻松。
-
好的,谢谢。在 jQuery.get() 的某个地方发现了它。你是说如果我改用jQuery,输出不显示的问题就解决了吗?
-
不,我是说使用 JavaScript 框架,如(jQuery、prototype、Yui 等)将使您的代码跨浏览器兼容。您可能会遇到仅在 IE 上发生的问题,但在 Firefox 上可以正常工作,因为每个浏览器中 JavaScript 引擎的实现不同(例如,innerHTML 可能在某些浏览器上工作,但在其他浏览器上失败)。 jQuery 提供了许多强大的 Selectors,让您可以轻松地操作 DOM 而不是 "getElementBy"。而不是检查 window.XMLHttpRequest jQuery 有 $.ajax 自动处理这部分。
标签: php javascript ajax xmlhttprequest blogger