Passing Variables from php page to another..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shraddha
Forum Newbie
Posts: 8
Joined: Mon Feb 23, 2009 6:38 am

Passing Variables from php page to another..

Post by shraddha »

Hi.

I have a page in php...having 2 variables, as suppose $var1 and $var2... n i want to pass these two variables to another page.. n display their values on that particular page using echo..

m using the foll. script:

$var1 = "Hiiii";
$var1 = "Helllooooo";
header("Location: temp.php?var1=$var1&var2=$Var2");

bt how do i display these values on the that page(temp.php) :(
n is the above syntax correct for passing values?????? :?: :?

pls help..

thnx in advance
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

Re: Passing Variables from php page to another..

Post by granite »

shraddha
Forum Newbie
Posts: 8
Joined: Mon Feb 23, 2009 6:38 am

Re: Passing Variables from php page to another..

Post by shraddha »

well thnx..

bt d prob is, these values are passed on to other page..bt nt displayed.....

script used in temp.php is:
<?php
if (isset($_POST['var1']))
{
$var1 = $_POST['var1'];
}
if (isset($_POST['var2']))
{
$var2 = $_POST['var2'];
}

echo "<h5>$var1</h5>";
echo "<br>";
echo "<h5>$var2</h5>";
?>

var1 is displayed bt var2 is not :(
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Passing Variables from php page to another..

Post by Benjamin »

Forum Rules wrote: 11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

Re: Passing Variables from php page to another..

Post by granite »

Yeah, you're calling the variable $_POST, but how did you pass the values $var1 and $var2 to it?
shraddha
Forum Newbie
Posts: 8
Joined: Mon Feb 23, 2009 6:38 am

Re: Passing Variables from php page to another..

Post by shraddha »

header("Location: temp.php?var1=$var1&var2=$Var2");
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: Passing Variables from php page to another..

Post by semlar »

$_GET
shraddha
Forum Newbie
Posts: 8
Joined: Mon Feb 23, 2009 6:38 am

Re: Passing Variables from php page to another..

Post by shraddha »

yeah i have tried $_GET too....but the same problem.. :(
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: Passing Variables from php page to another..

Post by semlar »

Well, $_GET pulls variables out of the url, so I don't know what to tell you.

file.php?var1=example -> $_GET['var1'] = example
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

Re: Passing Variables from php page to another..

Post by granite »

Try using $_SESSION instead:

Code: Select all

$_SESSION['var1'] = $var1;
$_SESSION['var2'] = $var2;
Then in your other php file,

Code: Select all

$var1 = $_SESSION['var1'];
$var2 = $_SESSION['var2'];
Simple like that.

BTW, $_POST['var2'] wasn't working for you because your header line sets the value $Var2 to var2, but the variable $Var2 doesn't exist. It shoud be $var2: PHP is case sensitive.
Post Reply