Using Multiple if...else statements

In the if…else statement, if the condition is true it prints a certain code, if its false it prints a different code - is there anyway you can have multiple conditions instead of just one? Here’s an example:


if ($c=="White"){
      echo "Your phone is not black";
   }else{
      echo "Your phone is not white";
   }

Is it possible to have all in one if…else statement? ($c==“White” && $m==“Blackberry” && $s==“Verizon”)? If so, how would you do that, using && just returns errors and having more than one “if” line also shoots errors?

Thanks!

One way to do it:

if(($c==‘White’) and ($m==‘Blackberry’) and ($s==‘Verizon’)){
echo “Do something here one”;
}elseif (($c==‘Black’) and ($m==‘Blackberry’) and ($s==‘Verizon’)){
echo “Do something here two.”;
}else{
echo “Does not match.”;
}

“Errors”. Please define, because the if conditional above would work as you expect it to.

Having this in the HTML editor:


<form method="post">
<select name="color">
<option value="select">-Select Option-</option>
<option value="White">White</option>
<option value="Black">Black</option>
</select>
</form>

<form method="post">
<select name="model">
<option value="select">-Select Option-</option>
<option value="iPhone">iPhone</option>
<option value="Blackberry">Blackberry</option>
<input type="submit" value="Submit" />
</select>
</form>

<? include('process.php'); ?>

And this in the PHP file:


<?php
if(isset($_POST['color'])){
$c=$_POST['color'];
$m=$_POST['model'];

if ($c=="White" and $m=="Blackberry"){
echo "Your phone is a white blackberry";
}else{
echo "Your phone is not a white blackberry";
}

}

?>

Displays the form/drop down lists like its supposed to but when you select something and click submit nothing happens. That’s the current problem I am having.

If I use the code that savister provided it will display the text, but it will display it before someone clicks submit AND it will not change no matter what you select.

Okay hold on there. Lets wind back to your HTML here a second…
You’ve got two distinct <form>'s there. When you submit one, it only submits the input fields in between it’s <form> and </form> tag. Meaning it submitted the Model information, but not the Color.

So lets make it one nice big form. I’ll even move your submit tag into the right spot :wink:

<form method="post">
<select name="color">
<option value="select">-Select Option-</option>
<option value="White">White</option>
<option value="Black">Black</option>
</select>

<select name="model">
<option value="select">-Select Option-</option>
<option value="iPhone">iPhone</option>
<option value="Blackberry">Blackberry</option>
</select>
<input type="submit" value="Submit" />
</form>

NOW what happens?

Whoops! Didn’t notice it was two separate forms. Making it one form fixed my problem, but now the submit button won’t work?


<form method="post">
<select name="model">
<option value="select">-Select Option-</option>
<option value="iPhone 1st Generation">iPhone 1st Generation</option>
<option value="iPhone 3G">iPhone 3G</option>
<option value="iPhone 3GS">iPhone 3GS</option>
<option value="iPhone 4">iPhone 4</option>
</select>
 
<select name="color">
<option value="select">-Select Option-</option>
<option value="White">White</option>
<option value="Black">Black</option>
</select>
</form>
 
<select name="service">
<option value="select">-Select Option-</option>
<option value="ATT">ATT</option>
<option value="Verizon">Verizon</option>
</select>
<input type="submit" value="Submit" />
</form>

<? include('process.php'); ?>


<?php
if(isset($_POST['color'])){
$m=$_POST['model'];
$c=$_POST['color'];
$s=$_POST['service'];

if ($m=="iPhone 1st Generation" and $c=="Black" and $s=="ATT"){
echo "You have a Black iPhone 1 on the ATT Network";

}

}

if ($m=="iPhone 3G" and $c=="White" and $s=="ATT"){
echo "You have a White iPhone 3G on the ATT Network";

}

?>

Thanks for the help, I really appreciate it.

Forget my last post, I figured it out - there were two end form tags.