Jump to content

how can i access $vars from different php files


naphelge

Recommended Posts

Hi gang, I am somewhat new to php but it's pretty fun (still) & easy to pick-up/follow. However I have run into a bit of a wall that I can't get around. I have read the PHP manual... I keep it very close by as well as the FAQs & couldn't find any info to help my problem, so here goes...

I have an 'some.html' page that asks the user to enter how many ingredients he needs to enter to be evaluated. When the 'Go' button (<input type=submit value=Go>) is clicked 'some.html' page then calls on 'some.php' file (<form name=someForm action=some.php method=post>).

some.php file embeds an HTML form in the PHP page & presents the user with form fields (tailored to how many the user specified in 'some.html') to be filled in. Once all applicable fields have been entered  the user then clicks on 'Evaluate' button (<input type=submit value=Evaluate>) then some.php page calls evalSome.php file (<form name=someForm action=evalSome.php method=post>).

evalSome.php file then uses info entered by user in some.php form fields by referencing info from mysql DB & pukes output to user. The user can now decide to click on a Submit button (<input type=submit action=sendSomeDb.php method=post>) which will send the results/output on screen to another mysql DB.

It is in this final php file (sendSomeDb.php) that I am having trouble. I need to access $vars from evalSome.php file that were evaluated in the previous file that called this current php file. But I can't seem access these $vars...

I have tried '$var = $_POST['$var']' which doesn't work (tried to 'echo "$var: ".$var."<br />";' which outputs no value) so I also tried '$var = $_POST['$var']' (which doesn't really exist in the preious page) which to no surprise didn't work as well as trying the $GET & $REQUEST globals which also to no surprise didn't work.

So since then I got to thinking perhaps I could pass the $vars I need in the sendSomeDb.php file using HTML hidden fields... ?><input type=hidden name=var value=$var><?php... when I echo $var in sendSomeDb.php I can see var (literally var, $var: var, not the value of $var) in the sendSomeDb.php... but again I am stumped how I could actually pass the value of $var... next I tried ?><input type=hidden name=var value="<?$var?>"><?php... now when I echo $var in sendSomeDb.php the result is NULL ($var: ).

How would I go about using $vars in a current php page that would need to be accessed from the previous php page that called the new php page?

Man, I hope that makes sense.
Your syntax for the hidden values was slightly off. You need the value of $ var, not the name $var in the hidden field, use something like this:
[code]
<input type="hidden" name="var" value="<?php echo $var ?>">
[/code]

You can also use sessions. In each script where you want to use sessions, put the
[code]<?php
session_start();
?>[/code]

statement at the top of the script, before any output is done.

When you want to store a value in a session variable do
[code]<?php
$_SESSION['var'] = $var;
?>[/code]

When you want to retrieve a value:
[code]<?php
$var = $_SESSION['var'];
?>[/code]

BTW, always quote all attributes in any HTML tag, if you don't you will eventually run into trouble.

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.