Solved

pass variable to another php file

Posted on 2003-11-06
6
509 Views
Last Modified: 2008-03-17
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.
0
Comment
Question by:skylabel
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9698576
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
0
 
LVL 1

Author Comment

by:skylabel
ID: 9698800
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
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9698841
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
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:skylabel
ID: 9699069
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>";

0
 
LVL 13

Expert Comment

by:lozloz
ID: 9699078
echo "<form action=\"uploademail.php\" method=\"get\" name=\"form\"><input type=\"hidden\" name=\"filename\"></input></form>";
0
 

Accepted Solution

by:
krozz56 earned 110 total points
ID: 9704878
file1.php

// proccesses
ob_start();

$_SESSION[file] = "test.php";

header("location: file2.php");

file2.php:


echo $_SESSION[file];
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question