Solved

pass variable to another php file

Posted on 2003-11-06
6
487 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:skylabel
Comment Utility
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
Comment Utility
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
Comment Utility
file1.php

// proccesses
ob_start();

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

header("location: file2.php");

file2.php:


echo $_SESSION[file];
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now