Jump to content

Recommended Posts

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..

 

Link to comment
https://forums.phpfreaks.com/topic/255990-please-help/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312269
Share on other sites

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')

Link to comment
https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312296
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312299
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312309
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312313
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/255990-please-help/#findComment-1312318
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.