Reputation: 22527
I have created a form that allows someone to create a quiz, all of the questions and the answers are stored in a database. You can see a simplified version of the form here:
http://jsfiddle.net/superuntitled/uaTtx/4/
Each question can have an infinite number of answers and only one answer can be correct. To denote the 'correct' answer, I am using radio buttons.
The problem that I am having is that only the selected radio button passes a value to my php code, see a sample $_POST
array of values (in this example, three answers for the question "What is a space station?"):
[text] => Array
(
[0] => A cosmic hotel
[1] => An interstellar depot
[2] => A closet organizer
)
[correct] => Array
(
[0] => 1
)
Even if the correct answer was "An interstellar depot", the first answer "A cosmic hotel" will be entered as the correct answer when I loop through this $_POST
array. This is because only a selected radio button will pass a value.
My radio input looks like this:
<input type="radio" name="answer[1][correct][]" value="1" />
The first set of brackets denotes the question it is associated with, and this number is incremented with each new question.
Does anyone have any ideas on how set the correct answer properly?
Upvotes: 0
Views: 4029
Reputation: 145482
Okay, I think I get it now. But I'm afraid there is no easy solution to your problem.
While the ..[]
syntax works for collecting the text fields in an array, there is no correlation between text inputs and radio inputs. You have to manually name the text fields answer[1][text][235]
and the radios answer[1][correct][235]
.
This requires an enumeration loop for the PHP output. And also obviously that the DHTML/AJAX "Add another answer/question" function has to parse the existing fields, and count the last number up.
If you want to retain the lazy solution, then you have to use type=checkbox
instead of type=radio
. That's the only way to have all fields present at submission, so the automatic ...[]
enumeration works for text fields and checkboxes.
You could use some DHTML/Javascript trickery there to ensure that only one checkbox per group is selected at any time. (= fake radio buttons)
Upvotes: 1
Reputation: 21
Could you not simply make a hidden form field that has the correct answer as its value and then check whether the value of a selected radio button is equal to that hidden input value?
For example:
<form>
<input type="hidden" name="correct_answer" value="1" />
<input type="radio" name="answer[1]" value="1" /> #1
<input type="radio" name="answer[1]" value="2" /> #2
<input type="submit" value="submit" />
</form>
I'm thinking that would solve your problem. You would need to check the value of the radio button against the value of the hidden form field. If they're the same, it's correct. Else, it's incorrect.
Hope that helps.
Upvotes: 0
Reputation: 522015
Yes, radio buttons only submit the checked value, that's the point.
Make your inputs like this:
<input type="radio" name="answer[1][correct]" value="1" />
Then you'll get arrays like this:
'answer' => array(
1 => array(
'text' => array( … ),
'correct' => 1
)
)
And the correct answer is easily picked with:
$array['answer'][1]['text'][$array['answer'][1]['correct']]
Upvotes: 3