Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

pass variable to another php file

Posted on 2003-11-06
6
Medium Priority
?
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 440 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

647 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