Jump to content

content of variable disappears


rrdemello
 Share

Recommended Posts

The following drives me crazy. The $message variable is built in some functions and then sent via e-mail. After it is initialized (1), the content of the variable $aut (that is passed as a parameter from a link) is added to the message string (2). When I check it with an 'echo' the output is correct. The problem starts when I enter the submit condition and further down in the code some functions. From this moment on, the content of $aut seems to be lost: nowhere any sign of the content of $aut ('Allied Ottevanger') anymore, not even when I add it again to $message. (3) But a simple string that is added to the $message, does stay and it sent in the e-mail. It seems there is something wromg with the scope. Maybe $aut must be a global? If so, how do I do this correctly? The global declarations I tried all ended in a T-variable error.Can anybody please help?(1)// The headline of the mail body $message = "Bestelling Stokerkade."; $message .= "\r\n\r\n";(2) $message .= $aut; echo $message; // this correctly gives the output: Besteling stokerkade. Allied Ottevanger.(3)// Processing the form dataif ($Submit) { $message .= $aut; (4) $message .= "samba";

Link to comment
Share on other sites

It's hard to say without seeing all the code involved, but this is how you declare a variable as global, you do this in the function you want to use the global variable in, before you use the variable:global $aut;
After experimenting for a few hours - I am just learning php - the solution came up. This page with the disappearing values contains an order form for a book. It is called from a book description page that sends author, title and price as parameters to the order form page. The action page of this form is the same page itself. That's why I thought declaration of a global varaible would solve the problem: everything is on the same page, isn't it. No! The action page of the form is $_SERVER[php_SELF], so it calls itself to handle the input. But apparently it treats itself just as if it calls another page. So it leaves itself and at that moment the parameter variables die. Then it re-enters. Global declarations don't work, because the page is seen as a new, different context.The confusing thing is that newly declared and initialized variables (not a parameter, they originate at this page) remain intact. What's even more surprising: if you add at this page a string to the value of the parameter variable, the added string part still lives, while the parameter value part dies. That is not very consistent, it seems to me.Anyway, thanks for the reaction.
Link to comment
Share on other sites

Parameter variables and other superglobals (the $_ variables) are just that - superglobal, and so remain intact through page changes. Their "contents" will disappear though.All variables expect those in the $_SESSION and $_COOKIE superglobals will disappear after location changes, as they are two different scripts and have no variable retentive properties (think like opening Word, typing something, not saving, and then restarting).Have a look at http://www.w3schools.com/php/php_cookies.asp and http://www.w3schools.com/php/php_sessions.asp

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...