Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 18th, 2004, 07:14 AM
Authorized User
 
Join Date: Feb 2004
Location: Karachi, , Pakistan.
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to knight
Default 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


Reply With Quote
  #2 (permalink)  
Old February 18th, 2004, 08:02 AM
Authorized User
 
Join Date: Sep 2003
Location: Cambridge, , United Kingdom.
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old February 18th, 2004, 11:37 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old February 18th, 2004, 12:45 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
:::::::::::::::::::::::::::::::::
Reply With Quote
  #5 (permalink)  
Old February 18th, 2004, 02:34 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #6 (permalink)  
Old February 18th, 2004, 03:18 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
:::::::::::::::::::::::::::::::::
Reply With Quote
  #7 (permalink)  
Old February 18th, 2004, 03:48 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
:::::::::::::::::::::::::::::::::
Reply With Quote
  #8 (permalink)  
Old February 19th, 2004, 07:07 AM
Authorized User
 
Join Date: Feb 2004
Location: Karachi, , Pakistan.
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to knight
Default

Thanks all specialy David and Rich thanks a lot

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined variable adel_88 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 July 9th, 2007 03:21 PM
Undefined Variable aspire Beginning PHP 4 July 12th, 2006 07:00 AM
Undefined variable najib Beginning PHP 4 July 1st, 2005 08:12 AM
undefined variable !! cody44 Beginning PHP 3 January 20th, 2005 05:47 PM
Undefined Variable fpolan Beginning PHP 9 July 15th, 2004 06:24 AM



All times are GMT -4. The time now is 11:37 AM.


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