I am making a survey form with multiple radio buttons. the questions are in a database and i pick the questions and display options as radio buttons
<div id="question">
<form id="pollform">;
<?php
$i=0;
while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){
$i++;
?>
<p><?php echo $ID.") ".$Question; ?></p>
<input type="radio" name="radio<?php echo $i;?>" value="1" /> <?php echo $one; ?><br />
<input type="radio" name="radio<?php echo $i;?>" value="2" /> <?php echo $two; ?><br />
<input type="radio" name="radio<?php echo $i;?>" value="3" /> <?php echo $three; ?><br />
<input type="radio" name="radio<?php echo $i;?>" value="4" /> <?php echo $four; ?><br />
<input type="radio" name="radio<?php echo $i;?>" value="5" /> <?php echo $five; ?><br />
<?php
}
?>
<input type="button" id="review-submit" value="Submit" onclick="pollsubmit();">
<form>
</div>
To process this and to send the selected answers using Ajax I used the following code
<script language=Javascript>
function pollsubmit(){
<?php
$dbquerry="SELECT `ID`, `Question`, `Form`, `one`, `two`,`three`,`four`,`five` FROM `pollquestions` Order by `ID`";
$result=mysql_db_query($dbname,$dbquerry);
?>
var answers = new Array();
var form1=document.getElementById("pollform");
<?php
$count=0;
while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){
$count++;
?>
for (var i=1;i<=5;i++) {
if(form1.radio<?php echo $count;?>[i].checked) {
answers[<?echo $ID;?>]= form1.radio<?php echo $count;?>.value;
}
}
<?php
}
?>
}
</script>
But this is giving me an Cannot read property 'checked' of undefined
though in both the places the name is radio1. whereas if i use only string say for example name="radio1"
it is working but if i use name="radio<?php echo $i;?>"
it is not working. Can you kindly help
The javascript for loop should start at 0. And for the value you'll need a [i].
for (var i=0;i<5;i++) {
if(form1.radio<?php echo $count;?>[i].checked) {
answers[<?echo $ID;?>]= form1.radio<?php echo $count;?>.value;
}
}
<script language=Javascript>
function pollsubmit(){
<?php
$dbquerry="SELECT `ID`, `Question`, `Form`, `one`, `two`,`three`,`four`,`five` FROM `pollquestions` Order by `ID`";
$result=mysql_db_query($dbname,$dbquerry);
?>
var answers = new Array();
var form1=document.getElementById("pollform");
<?php
$count=0;
while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){
$count++;
?>
for (var i=1;i<=5;i++) {
if(form1.['radio<?php echo $count;?>'][i].checked) {
answers[<?echo $ID;?>]= form1.['radio<?php echo $count;?>'].value;
}
}
<?php
}
?>
}
</script>
Edit:
for (var i=1;i<=5;i++) {
if(form1.['radio<?php echo $count;?>'][i].checked) {
answers[<?echo $ID;?>]= form1.['radio<?php echo $count;?>'].value;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.