neilfurry Posted January 29, 2012 Share Posted January 29, 2012 Im creating a quiz script..and this is the scenario, i have a form which contains the following 1 textfield for Question 4 textfields for choices and each of the choices has 1 radio button on the left.. This is what i need, lets say i have this Question : 1 + 1 o A. 1 o B. 2 o C. 3 o D. 4 when i submit this form and selected the radio button of the correct answer the output shows: INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('7922','11860','1','on') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('6467','11860','2','') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('23488','11860','3','') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('16633','11860','4','') as you can see "on" is in the wrong place it should go to the 2nd line with the correct answer.. Please help me.. Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/ Share on other sites More sharing options...
spiderwell Posted January 29, 2012 Share Posted January 29, 2012 we need to see the relevant source code to help you Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312268 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 here is my HTML Form: <form method="post" action="process/insert.php"> <div class="question">QUESTION : <input type="text" name="question" class="txtfield" /></div> <div class="choices">CHOICES : <br /><br /> <div class="flds"><input type="checkbox" name="radanswers[]" checked="checked" />A. <input type="text" name="txtanswers[]" class="txtfieldans" /></div> <div class="flds"><input type="checkbox" name="radanswers[]" checked="checked" />B. <input type="text" name="txtanswers[]" class="txtfieldans" /></div> <div class="flds"><input type="checkbox" name="radanswers[]" checked="checked"/>C. <input type="text" name="txtanswers[]" class="txtfieldans" /></div> <div class="flds"><input type="checkbox" name="radanswers[]" checked="checked"/>D. <input type="text" name="txtanswers[]" class="txtfieldans" /></div> </div> <div class="clear"></div> <div class="buttons"><input type="submit" name="ins_question" value="Insert Question" class="btn" /></div> </form> here is my php script: for($a=0; $a<=3; $a++){ $InsChoices = "INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('".rand()."','$questionID','".$_POST['txtanswers'][$a]."','".$_POST['radanswers'][$a]."')"; echo "<pre>"; echo $InsChoices; echo "</pre>"; } Thank you Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312269 Share on other sites More sharing options...
spiderwell Posted January 29, 2012 Share Posted January 29, 2012 its probably sometihng to do with having checked=checked in all the radio buttons, just have one or none checked Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312290 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 i already tried to remove all that checked so by default it doesnt have any check.. but still no luck..do you have any suggestions? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312292 Share on other sites More sharing options...
spiderwell Posted January 29, 2012 Share Posted January 29, 2012 none of the radio buttons have values, give them the values of a,b,c,d Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312294 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 i tried that and this is the result: INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('20541','7655','3','B') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('28722','7655','2','') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('12675','7655','1','') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('12032','7655','4','') The problem is letter B should be on the 2nd line as in: INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('28722','7655','2','B') Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312296 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 the value on the radio button should correspond the correct answer on the form with its textfield.. Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312298 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 for example Q: 1+1 Choices with radio btns: o [1] o [2] o [3] o [4] The numbers inside the bracket are in a text field, that whenever i ticked the correct answer it will be submitted to MYSQL DBASE together with the questions and the rest of the other choices Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312299 Share on other sites More sharing options...
spiderwell Posted January 29, 2012 Share Posted January 29, 2012 sorry i read radio button in your original text and didn't spot they were checkboxes in the code! Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312301 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 sorry, i just changed the radio to checkbox.. for better understanding you can see this link: http://neilfurry.net63.net/quiz/ Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312304 Share on other sites More sharing options...
spiderwell Posted January 29, 2012 Share Posted January 29, 2012 checkboxes allow multiple selections, which you wouldnt want for a maths question, so use radio buttons Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312307 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 im still getting the same output.. i used the radio button to tell that the certain textfield has the correct answer on the choices which will then be submitted to the MySQL DBASE : INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) and set it on the field "isCorrect" if the choice is the correct answer.. Thank you buddy.. Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312309 Share on other sites More sharing options...
spiderwell Posted January 29, 2012 Share Posted January 29, 2012 I tried a different approach, but its not tested, might work: changed radio to single value to match against a hard coded array key in answers <form method="post" action="process/insert.php"> <div class="question">QUESTION : <input type="text" name="question" class="txtfield" /></div> <div class="choices">CHOICES : <br /><br /> <div class="flds"><input type="radio" name="radanswers" value="A" checked="checked" />A. <input type="text" name="txtanswers['A']" class="txtfieldans" /></div> <div class="flds"><input type="radio" name="radanswers" value="B" />B. <input type="text" name="txtanswers['B']" class="txtfieldans" /></div> <div class="flds"><input type="radio" name="radanswers" value="C" />C. <input type="text" name="txtanswers['C']" class="txtfieldans" /></div> <div class="flds"><input type="radio" name="radanswers" value="D" />D. <input type="text" name="txtanswers['D']" class="txtfieldans" /></div> </div> <div class="clear"></div> <div class="buttons"><input type="submit" name="ins_question" value="Insert Question" class="btn" /></div> </form> here is my php script: foreach($_POST['txtanswers'] as $key =>$value){ if($key == $_POST['radanswers']) { $InsChoices = "INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('".rand()."','$questionID','".$value."','".$_POST['radanswers']."')"; } else { $InsChoices = "INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('".rand()."','$questionID','".$value."','')"; } echo "<pre>"; echo $InsChoices; echo "</pre>"; } You dont really need to put in A or B for the `iscorrect` column, it could be true or false. Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312313 Share on other sites More sharing options...
neilfurry Posted January 29, 2012 Author Share Posted January 29, 2012 i tried the code and here is the output: INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('787176855','1198368307','4','') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('1638744218','1198368307','5','') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('419012828','1198368307','6','') INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('456773631','1198368307','2','') as you can see the 'isCorrect' field still doesnt have any value. it should have at least TRUE on the 4th line as in INSERT INTO choices (`cID`,`qID`,`Itemchoices`,`isCorrect`) VALUES ('456773631','1198368307','2','TRUE') which means this is the correct answer.. Quote Link to comment https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.