Pass variable from a 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
katerina
Forum Newbie
Posts: 11
Joined: Wed May 14, 2008 1:41 pm

Pass variable from a page to another

Post by katerina »

Hi all,
I have two pages 1.php and 2.php.
Page 1.php (except the others) contains a variable $url and
a 'continue' button.
I would like when a user click on button to go to 2.php and print variable $url.

I am trying :

Code: Select all

1.php
print '<div align="center"> <a href="2.php?url2=$url">' ;
print '<input type="submit" value="continue" name="continue">';
print '</a> </div>'; 
 
2.php
 <?php
     $url2 = $_GET['url2'];
     echo "$url2";
?>
But the result is :
$url while I want the value of $url.

Could anyone help me ??

P.s. : I am trying form using post method and hidden button, but I have also problems.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: Pass variable from a page to another

Post by Verminox »

You are giong about this the wrong way. Don't try to hyperlink a submit button.

There are different ways to pass this variable around depending on your usage.

1. If you want to pass it as a GET request variable using only a hyperlink:

Code: Select all

<a href="2.php?url2=<?php echo $url; ?>">Continue</a>
2. If yo uwant to pass it as a GET request after clicking a button (uses the onclick event to perform a javascript redirect):

Code: Select all

<button onclick="window.location ='2.php?url2=<?php echo $url; ?>';">Continue</a>
3. You can store a hidden value in a form to send it with rest of the form inputs

Code: Select all

<form action="2.php" method="get">
<input type="hidden" name="url2" value="<?php echo $url; ?>" />
<input type="text" name="first_name" /><br />
<input type="text" name="last_name" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
All these methods will result in 2.php having access to $_GET['url2'] as whatever you printed $url as in the first page. (In the 3rd method $_GET['first_name'] and $_GET['second_name'] will also include whatever was typed in the form field)

4. You can use sessions. Start a session with session_start() at the top of both pages. Then set $_SESSION['url2'] = $url in 1.php, and it will be available as $_SESSION['url2'] in 2.php (assuming the user came from 1.php, because that's where it gets set).
katerina
Forum Newbie
Posts: 11
Joined: Wed May 14, 2008 1:41 pm

Re: Pass variable from a page to another

Post by katerina »

Unfortunately, it does not work .

If I am already in php, how can I write this line ?

Code: Select all

<a href="2.php?url2=<?php echo $url; ?>">Continue</a>
I am trying

Code: Select all

print '<a href="validator2.php?url2=<?php echo $url; ?>">Continue</a><br><br>';
 
but 'continue link' is hidden and when I click it url does not appear.


The form version leaves me in the same page, while the 2.php exists.


P.s. : I upload my code. Maybe someone can help me .
Thanks a lot!
Attachments
project.rar
(5.5 KiB) Downloaded 9 times
katerina
Forum Newbie
Posts: 11
Joined: Wed May 14, 2008 1:41 pm

Re: Pass variable from a page to another

Post by katerina »

please
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Pass variable from a page to another

Post by Jonah Bron »

Don't print the text. Just put it into the html:

Code: Select all

<a href="validator2.php?url2=<?php echo $url; ?>">Continue</a>
The <?php echo $url; ?> part outputs the value of $url right there. So, when the page (1.php), you actually end up with

Code: Select all

<a href="validator2.php?url2=http://some.url.com">Continue</a>
You cannot hyperlink a button.

P.S. Always use echo, not print. :)
katerina
Forum Newbie
Posts: 11
Joined: Wed May 14, 2008 1:41 pm

Re: Pass variable from a page to another

Post by katerina »

It works now!!!

Thanks a lot all
Post Reply