This is probably as simple as it gets, but I’m new to PHP and it’s not my forte, so please bear with me! I’m stuck!
I’ve got a form set up with a yes or no answer as a radio button. If the user clicks yes they are presented with two further radio button choices: a and b. If they had clicked no they would have been presented with radio buttons c and d.
Incidentally, I’ve got some JavaScript hiding the a and b DIV and the c and d DIV until either yes or no is clicked, just to keep things tidy. The no DIV, containing c and d disappears and is replaced with the yes DIV when yes is clicked after no, and vice versa. That’s all working fine.
My issue is with the PHP. It’s not likely, but if the user hits no, makes a choice, say d, then decides to change their answer to yes, then selects a, the script still returns d. I guess this is because d is still selected and appears last in the HTML flow.
What I would like my script to do is ignore c and d, even if they’ve been selected, if yes is selected. Similarly I’d like a and b to be ignored if no is selected.
A HTML looks something like this:
<label for="yes_or_no">Yes or No?</label>
<input type="radio" name="yes_or_no" id="yes" value="yes" />
<label for="yes">Yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" />
<label for="no">No</label>
<div id="yes">
<label for="a_or_b" id="a_or_b">A or B?</label>
<input type="radio" name="a_or_b" id="a" value="a" />
<label for="a">A</label>
<input type="radio" name="a_or_b" id="b" value="b" />
<label for="b">B</label>
</div>
<div id="no">
<label for="c_or_d" id="c_or_d">C or D?</label>
<input type="radio" name="c_or_d" id="c" value="c" />
<label for="c">C</label>
<input type="radio" name="c_or_d" id="d" value="d" />
<label for="d">D</label>
</div>
My PHP looks like this but it’s not working properly:
$yes_or_no = $_REQUEST["yes_or_no"] ;
$a_or_b = $_REQUEST["a_or_b"] ;
$c_or_d = $_REQUEST["c_or_d"] ;
if ($yes_or_no="yes" && $a_or_b!="") $result="$a_or_b";
if ($yes_or_no="no" && $c_or_d!="") $result="$c_or_d";
$feedback = "$result";
The variable $feedback is then emailed to me.
I hope that makes sense! Thanks for your help!
Martin.
Use
==for testing equality:I’m not sure if that will completely solve your problem, but perhaps it will get you started.
Oh, and you can do
$result = $a_or_binstead of$result="$a_or_b", but that won’t affect anything here.