getting answer from 5 random multiple choice in php -
i trying build web application generate 5 random question database along 4 possible answers. each question come own id. have stuck when user click submit button, direct result.php page shows number of right answers. dont know how match question id answer in database since there 5 questions @ same time (5 forms). here i've tried far
<script> function sender() { document.forms["form1"].submit(); document.forms["form2"].submit(); document.forms["form3"].submit(); document.forms["form4"].submit(); document.forms["form5"].submit(); return true; } </script> <?php include('db.php'); $sql = "select * quizzes order rand() limit 5"; $result = mysql_query($sql); while($data = mysql_fetch_array($result)){ echo ' <form id="form'.$i.'" action="result.php" method="post"> <table> <tr><td>'.$data['content'].'</td></tr> <tr><td><input type="radio" name="option" value="a">'.$data['a'].'</td></tr> <tr><td><input type="radio" name="option" value="b">'.$data['b'].'</td></tr> <tr><td><input type="radio" name="option" value="c">'.$data['c'].'</td></tr> <tr><td><input type="radio" name="option" value="d">'.$data['d'].'</td></tr> </table> </form> '; }} echo ' <input type="button" value="submit" onclick=javascript:sender()> '; ?>
i prefer herdoc syntax use mysql_num
in mysql_fetch_array
then add question id name of radio button
if questions numbered in column other id use instead e.g.
`select `number`,`
.
$sql = "select `id`,`content`,`a`,`b`,`c`,`d` quizzes order rand() limit 5"; mysql_query($sql); echo '<form action="result.php" method="post"><div>'; while($data = mysql_fetch_array($result,mysql_num)){ echo <<<eof <table><tr><td>$data[1]</td></tr> <tr><td><input type="radio" name="option$data[0]" value="a">$data[2]</td></tr> <tr><td><input type="radio" name="option$data[0]" value="b">$data[3]</td></tr> <tr><td><input type="radio" name="option$data[0]" value="c">$data[4]</td></tr> <tr><td><input type="radio" name="option$data[0]" value="d">$data[5]</td></tr></table> eof; } echo '<input type="submit" value="submit" /></div></form>';
get results:
to submitted answers result.php should start this:
foreach $_post $key => $value){ if(substr($key,0,6) == 'option`){ $id = intval(substr($key,6)); $answers[$id] = $value; } } var_export($answers);
if question id following 'option' not numeric, remove intval()
then question each answer:
foreach($answers $id => $answer){ $sql = "select .... `id` = $id"; //if non-numeric, add single quotes '$id' }
update
this should how basic form look:
<form action="result.php" method="post"><div> <tr><td><input type="radio" name="option1" value="a">answer a</td></tr> ... <tr><td><input type="radio" name="option5" value="a">answer d</td></tr> <input type="submit" value="submit" /></div></form>
the <form>
before <input type="submit" />
after submit
most problem unclosed set of double quotes
make sure double quotes closed these: "result.php"
, including name , value of each radio button.
if right click form , choose "inspect element" see:
the above firefox inspector
when click right facing arrow on <form>
you should see:
<form <div></div> </form
you should run web pages through w3c html markup validator:
Comments
Post a Comment