Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple radio button using php javascript ajax
    text
    copied!<p>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</p> <pre><code>&lt;div id="question"&gt; &lt;form id="pollform"&gt;; &lt;?php $i=0; while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){ $i++; ?&gt; &lt;p&gt;&lt;?php echo $ID.") ".$Question; ?&gt;&lt;/p&gt; &lt;input type="radio" name="radio&lt;?php echo $i;?&gt;" value="1" /&gt; &lt;?php echo $one; ?&gt;&lt;br /&gt; &lt;input type="radio" name="radio&lt;?php echo $i;?&gt;" value="2" /&gt; &lt;?php echo $two; ?&gt;&lt;br /&gt; &lt;input type="radio" name="radio&lt;?php echo $i;?&gt;" value="3" /&gt; &lt;?php echo $three; ?&gt;&lt;br /&gt; &lt;input type="radio" name="radio&lt;?php echo $i;?&gt;" value="4" /&gt; &lt;?php echo $four; ?&gt;&lt;br /&gt; &lt;input type="radio" name="radio&lt;?php echo $i;?&gt;" value="5" /&gt; &lt;?php echo $five; ?&gt;&lt;br /&gt; &lt;?php } ?&gt; &lt;input type="button" id="review-submit" value="Submit" onclick="pollsubmit();"&gt; &lt;form&gt; &lt;/div&gt; </code></pre> <p>To process this and to send the selected answers using Ajax I used the following code </p> <pre><code>&lt;script language=Javascript&gt; function pollsubmit(){ &lt;?php $dbquerry="SELECT `ID`, `Question`, `Form`, `one`, `two`,`three`,`four`,`five` FROM `pollquestions` Order by `ID`"; $result=mysql_db_query($dbname,$dbquerry); ?&gt; var answers = new Array(); var form1=document.getElementById("pollform"); &lt;?php $count=0; while(list($ID,$Question,$form,$one,$two,$three,$four,$five)=mysql_fetch_array($result)){ $count++; ?&gt; for (var i=1;i&lt;=5;i++) { if(form1.radio&lt;?php echo $count;?&gt;[i].checked) { answers[&lt;?echo $ID;?&gt;]= form1.radio&lt;?php echo $count;?&gt;.value; } } &lt;?php } ?&gt; } &lt;/script&gt; </code></pre> <p>But this is giving me an <code>Cannot read property 'checked' of undefined</code> though in both the places the name is radio1. whereas if i use only string say for example <code>name="radio1"</code> it is working but if i use <code>name="radio&lt;?php echo $i;?&gt;"</code> it is not working. Can you kindly help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload