Re: undefine variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Michael,

It seems that you are trying to use the $selected_state variable in the
code, but it hasn't been declared yet. To test if a variable exists, you
can use the isset() function.

Also, there is a quick way of writing your if/else check on one line.
Here's the changes:

foreach($state_list as $key=>$value)
{
        $sel = (isset($selected_state) && $selected_state ==
        $key)?'selected="selected"':'';
        echo "<option $sel value=\"$key\">$value</option>";
}
echo '</select>';
?>

(That's only two lines there inside the loop, just incase my email
client breaks the line because of wordwrap.)

Obviously, if your code worked before, but now is not, it might be that
some other change in your code is causing this error, and that
introducing my change will only mask a larger logic error. You should go
through your code to check where the $selected_state variable is meant
to be declared to make sure it really is.

There is another explanation. If you are not in control of your hosting,
it could be that they changed the error reporting level to show notices
as well, which I believe aren't turned on by default on all hosting
packages.


Ash
www.ashleysheridan.co.uk
--- Begin Message ---
I'm having a problem running a form script. It used to work before, but
now when I submit the form an error pops up and it won't write to the
database:


This is the error:

[Sun Aug 17 16:46:54 2008] [error] [client 127.0.0.2] PHP Notice:
Undefined variable: selected_state
in /srv/www/htdocs/djtommer/contact.php on line
243                                            


This is the code:
-------------

foreach($state_list as $key=>$value){
if($selected_state == $key){
$sel = 'selected="selected"';
}
else{
$sel = '';
}
echo "<option $sel value=\"$key\">$value</option>";
}
echo '</select>';
?>

----------


-- 
Michael S. Dunsavage


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--- End Message ---
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux