Page 1 of 1

Undefined variable:

Posted: Fri Jul 05, 2002 12:49 am
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

Re: Undefined variable:

Posted: Fri Jul 05, 2002 12:52 am
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

Re: Undefined variable:

Posted: Fri Jul 05, 2002 2:00 am
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

Posted: Fri Jul 05, 2002 8:46 am
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