Undefined variable:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
craginweb
Forum Newbie
Posts: 13
Joined: Fri Jul 05, 2002 12:49 am
Location: FWB Florida

Undefined variable:

Post by craginweb »

I am having a bit of a problem with a script I am running that takes its info from a database and lets the user update their profile. Everytime the user logs in through the edit form, the edit script returns: "Notice: Undefined variable: action".
This is the first instance of action:

Code: Select all

if ($action == "edit_two") {
$user = mysql_query("UPDATE personalize SET Password='$Password',Weather='$Weather' WHERE (Name='$Name')"); 
setcookie("site_user", $Name, time() + 31536000, "/"); 
    setcookie("site_pass", $Password, time() + 31536000, "/"); 
	showheader("Edit Success!");
I am not sure how I am supposed to define the variable "action" using PHP4.2.0, since it is not passed from the form until the user updates his profile.

This is the second instance:

Code: Select all

<FORM METHOD=POST ACTION="<?php echo $_SERVER&#1111;'PHP_SELF'] ?>">
<INPUT TYPE="hidden" NAME="Name" VALUE="<?php echo $info&#1111;'Name'] ?>"> 
<INPUT TYPE="hidden" NAME="action" VALUE="edit_two">
I know this has to do with the globals set to off, I just dont know what to do until the variable is passed from the form on the previos page, like the name and password ie:

Code: Select all

$Name = $_POST&#1111;'Name'];
$Password = $_POST&#1111;'Password'];
$action is not passed until the user updates the profile?
Can anyone help me to understand this?

Thanks
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: Undefined variable:

Post by protokol »

craginweb wrote:I know this has to do with the globals set to off, I just dont know what to do until the variable is passed from the form on the previos page, like the name and password ie:

Code: Select all

$Name = $_POST&#1111;'Name'];
$Password = $_POST&#1111;'Password'];
$action is not passed until the user updates the profile?
Can anyone help me to understand this?

Thanks
Well, you almost answered your own question. Since you make <input type="hidden" name="action" value="whatever">, the hidden values are also sent into the $_POST array.

So just as you got the $Name = $_POST['Name'] and $Password = $_POST['Password'], you can also say $action = $_POST['action']. Then you can check your if ($action == 'whatever') {} condition and it should work

Hopefully this fixes your problem
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Undefined variable:

Post by twigletmac »

craginweb wrote:I am not sure how I am supposed to define the variable "action" using PHP4.2.0, since it is not passed from the form until the user updates his profile.
Use isset() to check if the variable has been set before you try and do anything with it so:

Code: Select all

if (isset($_POST&#1111;'action']) && $_POST&#1111;'action'] == 'edit_two') &#123;
The notice appears because your error reporting is set to the highest level so if you try and do anything with a variable that isn't set yet (like compare it to something else in an if statement) PHP has a whinge.

Mac
craginweb
Forum Newbie
Posts: 13
Joined: Fri Jul 05, 2002 12:49 am
Location: FWB Florida

Post by craginweb »

Thanks twigletmac using isset() solved my problem, when I added it to the code the errors went away and things worked fine.

Thanks again
craginweb
Post Reply