CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 how global are global variables

Print topic Send  topic

Author Message
chris

Posts: 16
Posted: 06/29/2004, 12:23 PM

Hi,

I am trying to close a window programmaticaly (see http://forums.codecharge.com/posts.php?post_id=48531) - I know how to do it, by displaying window.close() in a label set to HTML - but I have the following issue - My application will accept an e-mail address, one hits a button and a mail + attachment will be sent; if the mail is sent correctly, I'd like the window to close (by using the above method) - I tried the following; I can control what is displayed on the label; either I display "mail not sent" or I display the window.close() : this works sofar. I used an if-statement, if $MailSent = true then close else display error.
I 've create the global $MailSent in all the areas where I 've added code: Initial set it to false, and set it true after the mail sent via the on-click - but the value doesn't change in display label part.

For information; the page has one form with a field and a button and the label used for the close/error-msg is not part of the form

Are variable really global, for the whole page, form or do I miss something ?

Chris
View profile  Send private message
peterr


Posts: 5971
Posted: 06/29/2004, 2:35 PM

May I ask how did you "create the global $MailSent in all the areas"?
I think that you should only create a global variable once outside of any events and then use the global keyword to declare it as global in the other areas. See http://www.google.com/search?q=php+declare+global+variable
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
chris

Posts: 16
Posted: 06/30/2004, 1:38 PM

Peter,

there must be something that doesn't work, or that I do not understand;
I will simplify my problem; on a given page, I have a record (not linked to a db) in a form, and two fields on the page, but outside the form;
I have no problem in making those fields, records, ... display data that I want.
Can I create a global variable, that can be accessed by all these above mentioned objects (label, textbox, records) ? Where do I initialize that variable ? How can I refer to it ?
In my case I defined it everywhere I needed it, but values are not passed; so as you mention, I probably have to define it outside of the functions on the highest level; where would that be ? on Page-level, or in one of the common files ?
Do have an exemple of code/project, where a variable is shared between several objects within a page ?
This must be possible ! My last fallback solution would be creating a session-variable, but I consider that un-elegant !

can you help, or point me in a certain direction ?

thx,

Chris

some code-snippets to explain what I do:

WHEN I CLICK A BUTON I'LL SEND AN EMAIL

/Custom Code @11-FF3FD3DA
// -------------------------
global $NewRecord1;
// Write your own code here.
global $ImageToSend;
global $MailSent;

//** load email class definition.

**** my code to send a mail ******

// $SendSuccess = $msg->Send();
$MailSent = true;



LATER IN A BEFORESHOW event:

//Custom Code @14-EAB3404B
// -------------------------
global $StatusField;
global $MailSent;
global $FirstPass;

if ($MailSent == true)
{
$StatusField->SetValue('<script> window.close(); </script>');
}
else
{
if ($FirstPass == true)
{
$FirstPass = false;
and so on ......
View profile  Send private message
peterr


Posts: 5971
Posted: 06/30/2004, 6:57 PM

I don't program in PHP too much, but I used global variables some time ago and it worked OK for me (can't find that example now). All I used was just the information from the Internet, thus for example based on http://www.zend.com/manual/language.variables.scope.php all needed to define a global variable is to assign a value to it like $a = 1;.
As you noticed this needs to be done outside of functions, therefore in CCS you can do it in the _events.php page, but butween events or above all functions, for example right below "//End BindEvents Method".
Hope this helps.
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
chris

Posts: 16
Posted: 07/02/2004, 5:48 AM

Peter,

after reading all I could find regarding the use of (global) variables in PHP, I understand how it works; I did a few tests and I can use global variables. My problem (I think) is caused by how CSS works:
I initialize the (global) variable (MailSent = false) in the beginnig, outside the functions as you told me;
I retrieve a session-variable with an e-mail address and display it in a text-box
I retrieve the file-name from the URL and display it in a label
I display a label-field, which is empty if MailSent : false else I display the window.close()
On-click of a button, I create an email, include the image as an attachment and send it (if successful I set a global variable MailSend to true)

*** here the problem starts:
I think that a CSS php program, after I click the button and processed the mail, will start from the beginning again, and thus re-initialize the global variable. This is the reason why within a CSS php-program, I cannot use it in the way I want to use it ?
Do you think this is correct ? If it is, I have to think of another mechanism (may use a session-variable)

thx,

CHris
View profile  Send private message
peterr


Posts: 5971
Posted: 07/02/2004, 11:43 AM

Chris,
Yes, this would explain it. I didn't realize that you needed to preserve some values before and after a form is submitted.
In this respect, CCS doesn't work differently than other PHP programs. There is usually one program that displays the page and then the page is submitted to another program, so you would loose the global variables as well.
A session or hidden field would be the way to solve this.
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.