This is a discussion on Variables in PHP4 within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello people I'm really new to PHP. Currently I'm trying to pass variables from one PHP file to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello people
I'm really new to PHP. Currently I'm trying to pass variables from one PHP file to another. I've the following 3 files: index.php ========= <?php include("./menu1.php") ?> <?php include("./menu2.php") ?> menu1.php ========= <?php $var1 = ($_GET["var1"]); $var2 = ($_GET["var2"]); echo "var1= $var1<br>"; echo "var2= $var2<br>"; echo "<a href=\"?var1=33\">Set var1</a><br><br>"; ?> menu2.php ========= <?php $var1 = ($_GET["var1"]); $var2 = ($_GET["var2"]); echo "var1= $var1<br>"; echo "var2= $var2<br>"; echo "<a href=\"?var2=55\">Set var2</a><br><br>"; ?> When pressing link in menu1.php, var1 is set to 33. When pressing link in menu2.php, var2 is set to 55. Everything seems to be ok. But when pressing link2, somehow I'm losing the value of variable var1. var1 is empty after pressing the link to set var2. Does anybody know how I can keep the value of var1 when pressing link2 (and vice versa). Any help appreciated. Sunny |
|
|||
|
Hello,
Am 05.02.2006 21:16 schrieb Sunny: > Hello people > > I'm really new to PHP. Currently I'm trying to pass variables from one > PHP file to another. > > I've the following 3 files: > > index.php > ========= > <?php include("./menu1.php") ?> > <?php include("./menu2.php") ?> > > menu1.php > ========= > <?php > $var1 = ($_GET["var1"]); > $var2 = ($_GET["var2"]); > > echo "var1= $var1<br>"; > echo "var2= $var2<br>"; > > echo "<a href=\"?var1=33\">Set var1</a><br><br>"; > ?> > > menu2.php > ========= > <?php > $var1 = ($_GET["var1"]); > $var2 = ($_GET["var2"]); > > echo "var1= $var1<br>"; > echo "var2= $var2<br>"; > > echo "<a href=\"?var2=55\">Set var2</a><br><br>"; > ?> > > When pressing link in menu1.php, var1 is set to 33. > When pressing link in menu2.php, var2 is set to 55. > > Everything seems to be ok. But when pressing link2, somehow I'm losing > the value of variable var1. var1 is empty after pressing the link to > set var2. Thats normal, because the script is executed again. Where should it get var1 from then. > Does anybody know how I can keep the value of var1 when pressing link2 > (and vice versa). Any help appreciated. I don't know what you want to do, but you could use cookies or MySQL or you have to append var1 to the link for var2. |
|
|||
|
What you want to do is in the link
echo "<a href=\"index.php?var1=33\">set var1</a>"; echo "<a href=\"index.php?var2=55\">set var2</a>"; The get the values the program needs to restart itself. You may also have to change index.php to use: $var1 = $_REQUEST['var1']; $var2 = $_REQUEST['var2']; Instead of the $_GET function. I am not positive as I am using PHP5. What I have been playing around with for a menu is: <?php $var=$_REQUEST['var1']; if ($var == "") { $var = "select0"; }; echo "<html><head><h1>$var</h1></head>"; echo "<br><br><br><br>"; echo "<br><br><a href=\"tst.php?var1=select1\">select1</a>"; echo "<br><br><a href=\"tst.php?var1=select2\">select2</a>"; echo "<br><br><a href=\"tst.php?var1=select3\">select3</a>"; echo "</html>"; ?> Now I am new to php so there may be an easier way. But what I am planing is to use var to determine what is displayed on the menu by doing a select from MySQL (that part not added yet and will replace the hard coded selections). Sunny wrote: > Hello people > > I'm really new to PHP. Currently I'm trying to pass variables from one > PHP file to another. > > I've the following 3 files: > > index.php > ========= > <?php include("./menu1.php") ?> > <?php include("./menu2.php") ?> > > menu1.php > ========= > <?php > $var1 = ($_GET["var1"]); > $var2 = ($_GET["var2"]); > > echo "var1= $var1<br>"; > echo "var2= $var2<br>"; > > echo "<a href=\"?var1=33\">Set var1</a><br><br>"; > ?> > > menu2.php > ========= > <?php > $var1 = ($_GET["var1"]); > $var2 = ($_GET["var2"]); > > echo "var1= $var1<br>"; > echo "var2= $var2<br>"; > > echo "<a href=\"?var2=55\">Set var2</a><br><br>"; > ?> > > When pressing link in menu1.php, var1 is set to 33. > When pressing link in menu2.php, var2 is set to 55. > > Everything seems to be ok. But when pressing link2, somehow I'm losing > the value of variable var1. var1 is empty after pressing the link to > set var2. > > Does anybody know how I can keep the value of var1 when pressing link2 > (and vice versa). Any help appreciated. > > Sunny |
|
|||
|
Sunny wrote:
> Hello people > > I'm really new to PHP. Currently I'm trying to pass variables from one > PHP file to another. <snip> > echo "<a href=\"?var1=33\">Set var1</a><br><br>"; > ?> <snip> > Does anybody know how I can keep the value of var1 when pressing link2 > (and vice versa). Any help appreciated. > You're not trying to pass the variable from file to file - you're trying to pass it from instance to instance. PHP is started up by the webserver, reads a file, generates a page then exits. If you want to supply several parameters they need to either be presented each time in the GET/POST variables e.g. echo "<a href=\"?var1=33&var2=55\">Set var1 and var2</a><br> Or store them serverside in the session (some data stored on the server, referenced by a cookie presented by the browser, and automatically saved when the PHP script finishes). I'd suggest you go find a cheap book on PHP to get you started. C. |
|
|||
|
|