Martin
Martin

Reputation: 351

Two Conditions in a PHP 'if' statement

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.

Upvotes: 2

Views: 3069

Answers (4)

Yasser Souri
Yasser Souri

Reputation: 1957

You can use js to clear any selection of a and b is no is selected and the same thing for when user selects yes.

Also you have some bugs in your php that others have mentioned.

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237975

if ($yes_or_no="yes" && $a_or_b!="") $result="$a_or_b";

The problem is with $yes_or_no = "yes". That is the assignment operator, not the comparison operator. It says "set $yes_or_no to yes, then return true" (this isn't exactly it, but it's close). You want an operator that says "return true if $yes_or_no is equal to yes".

Replace = with == and your code should work.

(There is another reason your code is broken here -- = has lower precedence than &&, whereas == has higher precedence, but that isn't the biggest issue.)

Upvotes: 0

Joey
Joey

Reputation: 354714

You're currently setting the variable $yes_or_no to the value in the if. Use == instead of =. One is assignment, the other is comparison.

Upvotes: 0

Matthew
Matthew

Reputation: 48304

Use == for testing equality:

if ($yes_or_no == "yes" && $a_or_b != "") $result="$a_or_b";
if ($yes_or_no == "no" && $c_or_d != "") $result="$c_or_d";

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_b instead of $result="$a_or_b", but that won't affect anything here.

Upvotes: 5

Related Questions