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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variables further than one php page

Status
Not open for further replies.

lauraSatellite

Technical User
Jul 22, 2004
35
IE
Hi,
I am trying to pass a variable ($textfileURL) from one 1.php page, to 2.php, and then on to 3.php.
While i know how to get the variable to the 2.php, i have
been unable to place the value of this variable within a form, and onto 3.php.
Within 3.php I have tried things like:
<input type="hidden" name="text" value=$textfileURL>

I have also tried setting the document.form.variable.value using javascript, but this hasnt worked either.
If anyone has any ideas on what i should be doing, please let me know.


 
How are you currently passing it from 1.php to 2.php?

You can use Sessions. At the top of each program, put the line "session_start();". In 1.php, store the variable:
Code:
$_SESSION['textfileURL'] = $textfileURL;
In the other programs, use the value:
Code:
$textfileURL = $_SESSION['textfileURL']

Ken
 
You left out the doublequotes in the input, as well as you misunderstood the name:

Code:
<input type="hidden" name="[b]textfileURL[/b]" value="[b]<?=$textfileURL?>[/b]">

I however suggest that you use post, as an action in the <form>:
Code:
<input type="hidden" name="[b]textfileURL[/b]" value="[b]<?=$_POST['textfileURL']?>[/b]">

ps. then you have to define the form:
Code:
<form action="page2.php" method="post">

I dont think you need sessions for this project, unless you want some kind of user validation or something else that expires.

Olav Alexander Mjelde
Admin & Webmaster
 
excellent thanks a million, this works with what you suggested.
I didnt realise that sessions were the only way of doing this, I had wanted to reassign variable values, but sessions seem much cleaner, although im new to them.
Thanks again.
-laura
 
I didnt realise that sessions were the only way of doing this

This is not true...

Try this:

page1.php:
Code:
Page 1: <hr />
<form action="page2.php" method="post">
  Value:&nbsp;<input type="text" name="test" value="enter something here" />
  <input type="submit" name="submit" value="submit" />
</form>

page2.php:
Code:
Page 2: <hr />
<form action="page3.php" method="post">
  Value:&nbsp;<<input type="text" name="test" value="<?=$_POST['test']?>" />
  <input type="submit" name="submit" value="submit" />
</form>

page3.php:
Code:
Page 3: <hr />
Value:&nbsp;
<?php
echo $_POST['test'];
?>

However, if you wish to use sessions, and it's non-vital data, I would recommend you FORCING the SID in the querystring! Or your users that have paranoid firewall might block the session cookie.

For forcing the SID, you do as follows:
<a href="page2.php?<?=SID?>" title="page 2">page 2</a>

or:
<form action="page2.php?<?=SID?>" method="post">

I dont think I would use sessions, if the data you want to transfer has nothing to do with some kind of authentication.

Olav Alexander Mjelde
Admin & Webmaster
 
You can use cookies or sessions. I never used sessions before, but it seems very helpful to keep arrays in there, and then just breaking them apart when analyzing them.
 
I think I have to point something out here:
Just because sessions is an easy way to share a variable, this does not mean you can write sloppy code and get away with it.

Do not write sloppy code.

If you do not need sessions, I would not use sessions.

* A session variable will work cross browsers
* A session variable will have a timeout

You can define global variables.
You can send variables thru:
* Querystring (less secure)
* <form> -> get or post
* <form> -> <input>

You also have another way to go:
Instead of going like:
page1->page2->page3

You can go:
page1->page1->page1

Based on variables, you can select what is to be done.

You can then switch the action.
If you do this, the variables will be kept inside this script, as this script will alwyas be the executing script, no matter what the action is.

eg:

step1
include("1.php");
step2
include("2.php");
step3
include("3.php");
default
include("1.php");

ps. this is not IRL code, look up the switch() function.
How do you set the action?

<input type="submit" name="action" value="step1" />
<input type="submit" name="action" value="step2" />
<input type="submit" name="action" value="step3" />
etc. etc.

You do not need to have one file per step, as you can also do:

<?php
if (1 > 2) {
?>
<form....
</form>
<?php
}
?>



Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top