Link to home
Start Free TrialLog in
Avatar of skylabel
skylabel

asked on

pass variable to another php file

php newbie here,

i need to pass the a variable (e.g. $filename) from file1.php to file2.php. Now I know I can include file1.php in file2.php and simply say $newfile = $filename.

But if file1.php is performs some processing operations, I don't wanna include it in file2, coz it (file1.php) will get executed again. Is there a way to send the $file to file2.php without having to include file1.php.

Thanks.
Avatar of lozloz
lozloz

if you have a link to file2.php then you can just do file2.php?filename=wee and access this variable through $_GET["filename"] on file2.php

if you have a form then you just add a hidden input, i.e.

<input type="hidden" name="filename" value="wee">

this can be accessed through $_GET["filename"] if your method is GET and $_POST["filename"] if it's POST

cheers,

loz
Avatar of skylabel

ASKER

thanks, but actually i want file1.php to send the variable $filename directly to file2.php without the user having to click on a link. So maybe a hidden form field in file1.php?

correct me if i'm mistaken....
$filename is dynamic and it's value is defined in file1.php already. So I can't define it in the form.

In file1.php, i'll include something like this?

<form action="file2.php" method="get" name="form">
<input type="hidden" name="filename"></input></form>

In file2.php
echo $_GET["filename"]

please clarify....thanks
can you tell me the purpose of file1? if you want to go straight to file2 without the user doing anything, use the header function:

header("Location: file2.php?filename=$filename");
exit;

the above redirects to file2 with the filename in the $_GET variable, but you can only use this if no actual HTML content has been output to the screen.. so no print/echo commands before the header function and you can't have an html template output either

loz
ok, file1 is actually a file upload script and file2 is a form to mail script containing the filename of the uploaded file. I tried a dummy echo "hello world"; and it works (i.e. the email script displays after the file upload) ... so I think the hidden form field idea would work...but I'm getting parsing errors for that. Can someone see where the error is

Basically I need PHP to render the following html
<form action="uploademail.php" method="get" name="form">
<input type="hidden" name="filename"></input></form>

This is what I've got (which has errors i think). Can someone correct the following...

echo "<form action=\"uploademail.php\"method=\"get\"name=\"form\"><input type=\"hidden\" name=\"filename\"></input></form>";

echo "<form action=\"uploademail.php\" method=\"get\" name=\"form\"><input type=\"hidden\" name=\"filename\"></input></form>";
ASKER CERTIFIED SOLUTION
Avatar of krozz56
krozz56

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial