if( $_GET) if else problem
need help...
the getting input form:
<select name="age">
<option value="25-30">25-30</option>
<option value="31-40">31-40</option>
<option value="41-50">41-50</option>
<option value="51-60">51-60</option>
</select>
example url
search/?age=25-30
the function php:
if ( $_GET['age'] !="25-30") $age = '("A")';
elseif ( $_GET['age'] !="31-40") $age = '("B")';
elseif ( $_GET['age'] !="41-50") $age = '("C")';
elseif ( $_GET['age'] !="51-60") $age = '("D")';
else ( $_GET['age'] !="25-30") $age = '("A"开发者_开发技巧)';
$search ="http://domain.com/?q='.$age;
the problem: the $age inside $search always returns A (or 25-30) even though i have selected other values (31-40, 41-50, 51-60)
please help..thanks
else
should represent a default value and ( $_GET['age'] !="25-30")
should be removed from it and here is what you should have actually:
if ( $_GET['age'] =="25-30") $age = '("A")';
elseif ( $_GET['age'] =="31-40") $age = '("B")';
elseif ( $_GET['age'] =="41-50") $age = '("C")';
elseif ( $_GET['age'] =="51-60") $age = '("D")';
else $age = '("A")'; // modify accordingly
You could do this cleaner using a switch
statement or an array with values, but before going into examples: Wouldn't it be easier to just set the value to A
, B
, C
... in the select
in the first place?
Re @Col.Shrapnels comment. Which one looks better. The if
soup above, or this?
switch($_GET["age"])
{
case "25-30": $age = "A"; break;
case "31-40": $age = "B"; break;
case "41-50": $age = "C"; break;
case "51-60": $age = "D"; break;
default: $age = "E"; break; // or whatever
}
I think you mean
==
not
!=
How do you think, what does !=
operator mean?
Anyway,
<select name="age">
<option value="A">25-30</option>
<option value="B">31-40</option>
<option value="C">41-50</option>
</select>
You are using the "Not Equal To" ("!=") Operator, and so, unless you select 25-30 from the list (which should result in (B), it will always stop at the first line.
For the full code, I would use:
switch( $_GET['age'] ) {
case '25-30' :
$age = '("A")'; break;
case '31-40' :
$age = '("B")'; break;
case '41-50' :
$age = '("C")'; break;
case '51-60' :
$age = '("D")'; break;
default :
$age = '("A")';
}
精彩评论