Maintaining global variables between forms/subform

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tenikiwon
Forum Newbie
Posts: 5
Joined: Thu Jun 12, 2003 9:19 am

Maintaining global variables between forms/subform

Post by tenikiwon »

I've been working on a form/subform combination.
When you first call the page you get a dropdown box for the customers.
After picking the customer and clicking GO it brings up the customer form and displays the customers information. From there you can update the customer information by clicking on another button.

My problem comes in when I want to display a subform for the customer. In this case. Each customer can have several sites. I can bring up another drop down box that displays the sites for the customer, but when I what to select the site (by Clicking another GO button) it looses the global I have setup for the customer ID.

Here's a brief of what I've tried.
1 > Two files customer.php and site.php. Lost value when switching forms.
2 > Functions customer() and site() using two different switch functions.

I should add that my stucture is similar to this...
function update_customer() <--used when customer alreay exsits
function save_customer() <--used when adding a customer
function get_customer() <---Runs the query and returns the data
function customer_form(results from get_customer) <--form's format
and the same setup for customer_site with two switch functions $site and $customer.
I've also read about using the $gobals["variable"] but think I don't fully understand it.

What is the best way to accomplish a form/subform connecting to a MySQL database?

Thanks in advace.

Andrew J. Nowicki
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Hidden feilds.

On your "sub-form" you need to add a hidden input feild (HTML) that contains your "customer ID", when you submit that "sub-form" your hidden input will be passed too.

The sub-form will probably need submitting to it's self (PHP_SELF) and you will need to ensure you keep setting your hidden feild after each submit (selection of each site).

Hope that helps.

Regards,
tenikiwon
Forum Newbie
Posts: 5
Joined: Thu Jun 12, 2003 9:19 am

Post by tenikiwon »

I'll give it a try. Along with this I've been trying to use a global variable and can get the output that I want by echo function() but can I reset the global variable or set another variable to hold that?
I have used hidden fields for other things, but I think I'm skipping over between the two forms that display data.

Thanks.
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

If it's in the global scope is should be availabale in the $GLOBALS array, check this array by using var_dump($GLOBALS) somewhere where you know your global should be.

Note: it will output alot of data so you will have to search through for your global variable/element name.

If your global is in there then simply (inside of outside your function) unset() the array element.

Regards,

To add:

To answer your other question, you need to be passing your variables between pages; the best way to do this, and since your using forms, is to keep your customerID in the HTML of the page (i.e. in a hidden feild).

Every time you submit your froms you need to pass all the data you need for the next page (even if it's the same page, by using PHP_SELF), otherwise you need to write the customerID to a cookie or store it in a session.
Last edited by cactus on Fri Jun 13, 2003 7:48 am, edited 1 time in total.
tenikiwon
Forum Newbie
Posts: 5
Joined: Thu Jun 12, 2003 9:19 am

Post by tenikiwon »

Thank you sir. So far so GOOD!!! :D
Post Reply