2012-04-08 50 views
0

我有一個簡單的輪詢系統來處理2個php文件。然而,它不適用於博主(prolly,因爲現在1文件是在HTML中)javascript不能輸出PHP的響應

更具體地說,民意調查的結果被註冊在我的分貝。它只是無法輸出響應。

繼承人我的代碼:

<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> 
+2

爲了上帝的緣故使用jQuery! ActiveXObject的?! – Songo 2012-04-08 11:08:11

+0

我該怎麼做?我很抱歉,我從教程 – 2012-04-08 11:25:23

+0

檢查了[jQuery](http://www.jquery.com)。 Goggling「_jQuery tutorials_」將會給出很多好的結果。 jQuery將使你的生活使用選擇器和內置函數更容易。 – Songo 2012-04-08 11:39:47

回答

1

SENDTO變量局部聲明在for循環。

0

我相信這是與innerhtml not working on blogger相同的問題,它不工作的原因是因爲XMLHttpRequest的目標必須位於同一個域。一旦代碼移植到Blogger,它將會中斷。 (在另一個問題上的完整答案)