我想创建一个民意调查,并在完成此调查后显示结果。但是,答案似乎没有出现......
这是我的page1.php:
<div id="poll">
<?php
echo "<form onsubmit='getVote(option_ID); return false;' >" ;
echo "<p><b> $question_content </b></p>" ;
$poll_option_selection = "SELECT option_ID, option_content FROM poll_options WHERE question_ID='$question_ID'" ;
$poll_option_result = mysqli_query ($connect,$poll_option_selection) ;
foreach ($poll_option_result as $poll_option) {
$option_ID = $poll_option ['option_ID'] ;
$option_content = $poll_option ['option_content'] ;
?><script type="text/javascript">var option_ID = <?php echo json_encode($option_ID) ; ?> ;</script><?php
echo "<input type='radio' id='$option_ID' value='option' required' /> $option_content</br>" ;
}
echo "<p><input type='submit'></p></form>" ;
?>
</div>
<script type="text/javascript">
var user_ID = <?php echo json_encode($user_ID) ; ?> ;
function getVote(option_ID) {
var option_ID = document.querySelector("input[value='option']:checked").id;
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 (option_ID.readyState==4 && option_ID.status==200) {
document.getElementById("poll").innerHTML=option_ID.responseText;
}
}
xmlhttp.open("GET","./core/vote.php?user_ID="+user_ID+"&option_ID="+option_ID,true);
xmlhttp.send();
}
</script>
这里是带有结果的page2.php,由getVote(option_ID)
调用:
<?php
echo "<p><b> $question_content </b></p>" ;
$option_selection = "SELECT option_content, option_score FROM poll_options WHERE question_ID='$question_ID' ORDER BY option_score DESC" ;
$option_result = mysqli_query ($connect,$option_selection) ;
foreach ($option_result as $option) {
$option_content = $option ['option_content'] ;
$option_score = $option ['option_score'] ;
echo "
<table>
<th><td> $option_content : </td>
<td><b> $option_score votes </b></td></th>
</table>
" ;
}
echo "<p>Vous devez attendre 6 heures afin de pouvoir revoter</p>" ;
?>
有人可以用一个例子向我解释错误吗?因为我是初学者......谢谢你。
答案 0 :(得分:0)
您正在检查字符串,即元素ID,因为它的readyState不起作用,您必须检查XMLHttpRequest对象。
这就是你正在做的事情
function getVote(option_ID) {
var option_ID = document.querySelector("input[value='option']:checked").id;
xmlhttp.onreadystatechange = function() {
if ( option_ID.readyState == 4 && option_ID.status == 200 ) {
// ^ THATS A STRING ?
}
你想做的是
var user_ID = <?php echo json_encode($user_ID) ; ?>;
function getVote(option_ID) {
var option_ID = document.querySelector("input[value='option']:checked").id;
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("poll").innerHTML = xmlhttp.responseText;
}
}
var data = "?user_ID=" + encodeURIComponent(user_ID) +
"&option_ID=" + encodeURIComponent(option_ID);
xmlhttp.open("GET", "./core/vote.php" + data, true);
xmlhttp.send();
}