p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning PHP (http://p2p.wrox.com/forumdisplay.php?f=95)
-   -   Undefined Variable Some Help! (http://p2p.wrox.com/showthread.php?t=9499)

knight February 18th, 2004 07:14 AM

Undefined Variable Some Help!
 
i m using and testing the code below
---------------------------------
<HTML>
<HEAD></HEAD>
<BODY>
<FORM METHOD=POST ACTION="checkbox.php">
Have you ever eaten haggis before?
<INPUT NAME="Choice" TYPE="Checkbox">
<BR>
<BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML
----------------------------------------
and checkbox.php is as under

<HTML>
<HEAD></HEAD>
<BODY>
<?php
echo $Choice;
?>
</BODY>
</HTML>
----------------------------------------------
the problem is when i click submit button the error

Notice: Undefined variable: Choice in d:\inetpub\wwwroot\php\checkbox.php on line 5

is displayed what is the error please resolve it

thanks
[?]


CFerthorney February 18th, 2004 08:02 AM

It sounds like you have global variables turned off. Don't panic this is a good thing. To access the data from your form you need to use the following:
Code:

<?php
$Choice = $_POST['Choice'];
echo $Choice;
?>

As of PHP 4.2 global variables was turned off by default, as it was seen that global variables was a security risk.

HTH


---
David Thorne, Student
UK

sami February 18th, 2004 11:37 AM

Hi Knight,
I actually think the 'problem' is not really a problem as the error message is saying that you have a variable undefined, which is true as if you don't tick the checkbox the variable is not defined. Whereas if you where to check to see if the variable exists (using the 'isset' function )before you echo out the value, you would not receive the error message, try this:

<?php
if (isset($HTTP_POST_VARS['Choice']))
{
echo $HTTP_POST_VARS['Choice'];
}
?>

You could also remove this sort of error by changing the line:

error_reporting= E_ALL;

to not display all error messages but I don't recommend this in a testing phase.

Hope this helps
Sami

richard.york February 18th, 2004 12:45 PM

Well David is correct in pointing out that this is a problem with the register_globals setting, however, Sami, $HTTP_POST_VARS, is deprecated and should never be the recommended method, $_POST should be used instead. In fact the old $HTTP_*_VARS may not even exist in PHP 5... so to keep your script working with the latest version of PHP, use the short superglobal counterparts.

All this and more is mentioned at this URL:
http://www.php.net/manual/en/languag...predefined.php

If register_globals is turned off then the global $Choice will not be defined, and as such under error_reporting E_ALL will throw a notice level error, in this case a variable that is used before being defined. So it clearly indicates a problem, since Knight clearly needs a varible defined from his form. Predefined variables to the rescue!

Use...
echo $_POST['Choice'];

Or better yet...
If choice exists and it isn't empty, output it.. otherwise throw an error.

if (isset($_POST['Choice']) && !empty($_POST['Choice']))
{
    echo $_POST['Choice'];
}
else
{
    echo 'Error: you did not indicate the eating of haggis!';
}

isset and empty are basic language constructs (not functions), isset returns a boolean value based on whether the variable passed to it exists or not. empty returns a boolean value based on whether the vairable passed to it contains an 'empty' value if true the variable contains one of 0, '0', null, false, 'false'.

See:
http://www.php.net/isset
http://www.php.net/empty

Loads of information on this very frequently asked question:
http://www.google.com/search?q=regis...e:p2p.wrox.com

Regards, ; )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

sami February 18th, 2004 02:34 PM

Hi Rich,
Thanks for pointing out the $HTTP_*_VARS future compatability problem, but I have to code like this as my host is running 4.06. I know you have commented on this before here:

http://p2p.wrox.com/topic.asp?TOPIC_ID=6147

with a solution, but am unsure how (or where) to implement your code:

<?php

if (phpversion() <= '4.1.0')
{
    if (isset($HTTP_GET_VARS)) $_GET = $HTTP_GET_VARS;
    if (isset($HTTP_POST_VARS)) $_POST = $HTTP_POST_VARS;
    if (isset($HTTP_COOKIE_VARS)) $_COOKIE = $HTTP_COOKIE_VARS;
    if (isset($HTTP_POST_FILES)) $_FILES = $HTTP_POST_FILES;
    if (isset($HTTP_SESSION_VARS)) $_SESSION = $HTTP_SESSION_VARS;
    if (isset($HTTP_SERVER_VARS)) $_SERVER = $HTTP_SERVER_VARS;
    if (isset($HTTP_ENV_VARS)) $_ENV = $HTTP_ENV_VARS;

# No need to recreate the $GLOBALS var, has existed since PHP 3
}

?>

in order to preserve portability should I use this in an include file or place at the start of every piece of php code?

Back to the point, I was making Knight aware that he needed to check for the existence of a variable using 'isset' before 'echoing' the result.

Sami

richard.york February 18th, 2004 03:18 PM

Yes I recall that discussion.

If you have the ability to make changes to php.ini, write the above code to a separate file and use the auto_prepend_file directive.

auto_prepend_file = path/to/file.php

If you do not have access to php.ini, but are using apache and have the ability to use .htaccess files you may also place the configuration there. This will work on a per-directory basis.

The syntax for .htaccess is:
php_value auto_prepend_file path/to/file.php

I'm guessing you could do the same for httpd.conf, if neccessary.

So essentially doing this will automatically execute this code before any other PHP script is executed, bringing the nifty pseudo-superglobals into the current script's scope.

The comparison there in the conditional expression doesn't really work as you might expect it to.. as Nik mentioned in the other thread... as it is written you would expect PHP to treat '4.1.0' as an integer, except the extra dot defies the definition of a real number, so PHP would, eductated guess here, look at 4.1 as the floating integer. Which would be fine and dandy and even work for the intended purpose. But for things to make sense logically speaking, we'll do this other method which does not rely on the bunk logic.

Since version_compare was introduced in the same version of PHP as were the shortened super-global arrays, you could simply test for existence of that function.

Code:

<?php

// autoprepend.php

if (!function_exists('version_compare'))
{
    if (isset($HTTP_GET_VARS))    $_GET    = $HTTP_GET_VARS;
    if (isset($HTTP_POST_VARS))    $_POST    = $HTTP_POST_VARS;
    if (isset($HTTP_COOKIE_VARS))  $_COOKIE  = $HTTP_COOKIE_VARS;
    if (isset($HTTP_POST_FILES))  $_FILES  = $HTTP_POST_FILES;
    if (isset($HTTP_SESSION_VARS)) $_SESSION = $HTTP_SESSION_VARS;
    if (isset($HTTP_SERVER_VARS))  $_SERVER  = $HTTP_SERVER_VARS;
    if (isset($HTTP_ENV_VARS))    $_ENV    = $HTTP_ENV_VARS;
}
?>

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

richard.york February 18th, 2004 03:48 PM

I would also urge your ISP to upgrade their version of PHP! You're going to run into a plethora of things that are available now that weren't available then, functions that used to return NULL but now return BOOL. 4.2.0 containing many enhancements.

If they are worried about compatibility then tell them to turn on register_globals and modify error_reporting. Staying in the PHP 'stone age' isn't the best place to be IMHO to learn a rapidly evolving language.

Short list of things that you cannot use in PHP < 4.2.0

PEAR or any PEAR package!
Superglobals

... actually have a look at the changelog:
http://www.php.net/ChangeLog-4.php#4.2.0

There are a multitude of enhancements, bug fixes, etc, introduced after PHP 4.2.0 as well.

If your ISP is still unwilling to upgrade PHP, then I would strongly urge you to consider a change in service providers!

Also Nik has an interesting set-up on his localhost for testing PHP scripts on multiple versions of PHP... with a set-up like that you can adjust for the cutting edge and for cobwebs (haha!).

See: http://p2p.wrox.com/topic.asp?TOPIC_ID=7987

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

knight February 19th, 2004 07:07 AM

Thanks all specialy David and Rich thanks a lot



All times are GMT -4. The time now is 06:32 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.