[英]What is wrong with the Ajax code and the PHP radio input?
In this code I can not get the radio input value to use for updating the database: 在此代码中,我无法获取用于更新数据库的单选输入值:
<script type="text/javascript">
function getVote() {
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("poll").innerHTML=xmlhttp.responseText;
}
}
var vote = $('input[name=optionid]:checked').val();
xmlhttp.open("GET","poll_vote.php?vote="+vote);
xmlhttp.send();
}
on the same file the code is: 在同一文件上的代码是:
$options = mysql_query ("select * from options where pollid='poll'");
while ($row1 = mysql_fetch_array($options))
{ $id = $row1['id'];
$option = $row1['choice'];
$votes = $row1['votes'];
echo '<form id="poll">';
echo '<input type="radio" name="optionid" value="'.$id.'" onclick="getVote()" /><span>'.$option.'</span><br />';
}
echo '</form>';
How to get checked radio value to use it on the other page "poll_vote.php"? 如何在另一页“ poll_vote.php”上获取检查的单选值以使用它? By the way options used by the input radio are 3.
顺便说一下,输入收音机使用的选项是3。
Obviously I do not know if there are other problems , but with these two changes should be tested what happens, and maybe it works. 显然,我不知道是否还有其他问题,但是应该对这两个更改进行测试,以了解会发生什么,并且也许行得通。
Javascript Java脚本
function getVote() {
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("poll").innerHTML=xmlhttp.responseText;
}
}
var vote = $(this).attr('value');
alert('your vote: ' + vote); // WITH THIS CHECK IF THE VOTE IS CORRECT
xmlhttp.open("GET","poll_vote.php?vote="+vote);
xmlhttp.send();
}
PHP 的PHP
$options = mysql_query ("select * from options where pollid='poll'");
echo '<form id="poll">';
while ($row1 = mysql_fetch_array($options))
{ $id = $row1['id'];
$option = $row1['choice'];
$votes = $row1['votes'];
echo '<input type="radio" name="optionid" value="'.$id.'" onclick="getVote()" /><span>'.$option.'</span><br />';
}
echo '</form>';
The problem I notice with the is this line, echo '<form id="poll">';
我注意到的问题是这一行,
echo '<form id="poll">';
, is repeatedly printed as many as the return number of rows. ,重复打印与返回的行数一样多。
In the browser you have, 在浏览器中,
<form id="poll">
<input type="radio" name="optionid" value="<?php echo $id; ?>" onclick="getVote()">
<form id="poll">
<input type="radio" name="optionid" value="<?php echo $id; ?>" onclick="getVote()">
<form id="poll">
<input type="radio" name="optionid" value="<?php echo $id; ?>" onclick="getVote()">
<form id="poll">
<input type="radio" name="optionid" value="<?php echo $id; ?>" onclick="getVote()">
<form id="poll">
<input type="radio" name="optionid" value="<?php echo $id; ?>" onclick="getVote()">
.
.
.
It happens you a number of repeated forms, before this closing tag 在此结束标记之前,您发生了许多重复的表单
</form>
I offer you arrange your code in this way, for better analysis 我建议您以这种方式安排代码,以进行更好的分析
<?php $options = mysql_query ("select * from options where pollid='poll'"); ?>
<form class="" id="poll" action="" method="post">
<?php while ($row1 = mysql_fetch_array($options)): ?>
<?php
$id = $row1['id'];
$option = $row1['choice'];
$votes = $row1['votes'];
?>
<input type="radio" name="option<?php echo $id; ?>" value="<?php echo $id; ?>" onclick="getVote()">
<span><?php echo $option; ?></span>
<?php endwhile; ?>
</form>
And will print your form like this, 并会像这样打印您的表格,
<form id="poll">
<input type="radio" name="option1" value="<?php echo $id; ?>" onclick="getVote()">
<input type="radio" name="option2" value="<?php echo $id; ?>" onclick="getVote()">
<input type="radio" name="option3" value="<?php echo $id; ?>" onclick="getVote()">
<input type="radio" name="option4" value="<?php echo $id; ?>" onclick="getVote()">
</form>
notice this part name="option<?php echo $id; ?>"
? 注意这部分
name="option<?php echo $id; ?>"
吗? I made it like that so that you can have unique variable for every radio button, and don't forget to make your form like this <form class="" id="poll" action="" method="post">
so that you sent data via method post. 我这样做是为了让您可以为每个单选按钮设置唯一的变量,并且不要忘记使您的表单像这样
<form class="" id="poll" action="" method="post">
您是通过方法发布发送数据的。
It may not be the solution, I hope this would help. 可能不是解决方案,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.