|
Posted by Rik Wasmus on 09/01/07 19:45
On Sat, 01 Sep 2007 20:25:08 +0200, skim1114 <skim1114@gmail.com> wrote:=
> I am embarrassed to ask this, but I've worked on this for hours and I
> can't seem to find what the error is. I am trying to teach myself php
> and I am in the very early stages of understanding it. I have a book
> that I am walking through. I created a form to validate...all pieces
> I've coded work, except the piece for the age drop down. There are 3
> options, 0-20, 30-60, and 60+. What in the code is wrong here?
What is actually going wrong? And what is in $_REQUEST['age'] if you che=
ck =
it?
> I
> understand this might not be the most efficient way to code this, but
> like I said, I'm just starting.
>
> I'm sure anyone with php knowledge could help me out with this in a
> second.
>
> Thanks in advance. Code below:
>
> ---------
>
> if (isset($_REQUEST['age'])) {
Its preferable to explicitly state where the value comes from, so =
$_GET['age'], $_POST['age'] etc.
> $ =3D $_REQUEST['age'];
>
> if ($age =3D=3D '0-29') {
> echo '<p>Your age selection was between 0-29</p>';
> }elseif ($age =3D=3D '30-60') {
> echo '<p>You age selection was between 30-60</p>';
> }elseif ($age =3D=3D '60+') {
> echo '<p>You age selection was between 60+</p>';
> }else{
> $age =3D NULL;
> echo '<p><font color=3D"red">You forgot to select you age!</font></p>=
';
> }
You might want to look into a switch statement (I'll assume POST, change=
=
if this isn't the case):
<?php
$age =3D false;
if(isset($_POST['age']){
switch($_POST['age']){
case '0-29':
case '30-60':
case '60+':
$age =3D $_POST['age'];
echo "<p>Your age selection was {$_POST['age']}</p>";
break;
default:
echo '<p style=3D"color:red;">'.htmlspecialchars($_POST['age']).' is =
not =
a valid selection.</p>';
=
}
}
if($age=3D=3D=3Dfalse) echo '<p style=3D"color:red;">You forgot to selec=
t you =
age!</p>';
?>
Or a 'valids' array:
<?php
$valids =3D array('0-29','30-60','60+');
if(isset($_POST['age']) && in_array($_POST['age'],$valids){
echo "<p>Your age selection was {$_POST['age']}</p>"; =
} else {
echo '<p style=3D"color:red;">Please select a valid age.</p>'
}
?>
-- =
Rik Wasmus
My new ISP's newsserver sucks. Anyone recommend a good one? Paying for =
quality is certainly an option.
[Back to original message]
|