Variables in PHP4

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


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-05-2006
Sunny
 
Posts: n/a
Default Variables in PHP4

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

Reply With Quote
  #2 (permalink)  
Old 02-05-2006
Johannes Wienke
 
Posts: n/a
Default Re: Variables in PHP4

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.
Reply With Quote
  #3 (permalink)  
Old 02-05-2006
David Speight
 
Posts: n/a
Default Re: Variables in PHP4

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


Reply With Quote
  #4 (permalink)  
Old 02-05-2006
Colin McKinnon
 
Posts: n/a
Default Re: Variables in PHP4

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.


Reply With Quote
  #5 (permalink)  
Old 02-09-2006
Drakazz
 
Posts: n/a
Default Re: Variables in PHP4

maybe a freebook... http://php.net/sessions ?

Reply With Quote
  #6 (permalink)  
Old 03-07-2006
comp.lang.php
 
Posts: n/a
Default Re: Variables in PHP4

Thanks guys, my first session works!

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:35 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0