Radio Inputs and PHP

I got problems with radio inputs and PHP. I make a quiz and I set 4 optional answers each question. To make inputs not multi select able I gotta set their names the same. Unfortunately, PHP reads them by names and it makes PHP fail. How can I avoid it? I set optional number 1-4 for each answer. My example website and code: http://tourney.adrenalinex.co.uk/max-o-meter/index.php

[php]<?php

$q1_a1 = $_POST[‘q1_a1’];
$q1_a2 = $_POST[‘q1_a2’];
$q1_a3 = $_POST[‘q1_a3’];
$q1_a4 = $_POST[‘q1_a4’];

$score = 0;

if ($selected_radio = $q1_a1) {
$score = $score+4;
} else if ($selected_radio = $q1_a2) {
$score = $score+3;
} else if ($selected_radio = $q1_a3) {
$score = $score+2;
} else if ($selected_radio = $q1_a4) {
$score = $score+1;
}

echo $score;

?>[/php]

[code]

  1. Question number one:

    Answer 1

    Answer 2

    Answer 3

    Answer 4

[/code]

It looks like it works to me I don’t under stand the issue your having.

Well, normally, you do not set up radio buttons that way. Normally, you assign the same group of buttons with the same name. You just change the value’s of them. In this way, you have the value automatically and can drop most of the code you have.

Answer 1

Answer 2

Answer 3

Answer 4

$answer = $_POST[‘answers’]; // results of this is 1 to 4 (remove all the if’s checking the values…)
$score = $score + $answer;

So much easier than all those if’s and else-if’s and you only read the radio’s once.
You must remember that radio buttons were designed to “group” selections and pick one from all.
In your version, you have four separate radio’s. In this case, you have to handle each as it’s own item.

Hope that makes sense. good luck…

Oh, I get it now.

Andrew, glad you got it… It is actually quite simple and Zakhary was over-thinking it. We all do that all the time. Now, I hope Zakhary got it, too…

CYA both in the bitstream...  (That's really all this is...)
Sponsor our Newsletter | Privacy Policy | Terms of Service