Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error passing variavle from one page to another 1

Status
Not open for further replies.

james0264

Programmer
Mar 1, 2005
52
GB
I'm having big problems with passing a variable from one page to another. I have file (index.php) that sets a few variables then links via include() to an another file (layout.php). What I am having trouble is passing the varible onto the layout page as it says no variable has been set when I turn on the error reporting. I have tried turning on 'register_globals' in ini_set and tried using the $_GET['variable_name'] but it still errors.

In index.php ...

<?php
$variable_name = variable_string;
// and other variables ...
include (layout.php');
?>


In layout.php ...

<?php
ini_set ('register_globals', 1);
$variable_one = $_GET['variable_name'];
print ($variable_one);
// and other variables ...
?>

Can you please help me (by showing me a relevant link or providing a solution) with this terrible problem? Thanks in advance for any support.
 
If you are including one page within the other, that is not passing variables from one to another. you're simply inserting the code from one script within another. Simply printing out a variable that is set in the main file in the included file should be no problem. This works as expected:
Code:
foo.php
<?php
  $foo = "bar";
  include 'bar.php';
?>

bar.php
<?php
  echo $foo;
?>
Calling foo.php outputs 'bar'. If you are calling the variable from a function, that might cause it to not be available. In that case, you should consult the variable scope.
 
Err yeah, what's what I'm having trouble with. You got what I'm trying to do but every variable I try to pass, a message similar to the following comes up.

Notice: Undefined variable: skiset in /home/james0264/public_html/layout.php on line 22

Has it got something the include function as I always use the parenthesis when I include files and outputs:
Examples ...
include ('print ('some_text');

... or has it got something to do with using echo instead of print as I always used print from the begining.
 
Don't use a URL when you use the include() function. Just use the plain file. Whe you use a URL, you are not really incluing anything...

Ken
 
Lol, just missed the second part of your post...
No, I'm not calling from a function. It's not in any if, else etc. functions either.
 
Oh my god!
Thank you so much Kenrbnsn!
I was trying to work out why for the past month and you just came up within a minute?!?!?!
Thank you Ken, sooo much!
Here's a star! :D
 
Like Ken said, you shouldn't use a full address when including files. If you do, your in the included file will first try to be executed (hence cannot find the variables) and the result of the script will be included. You want to include raw script not the output of the script and should use relative links (without http).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top