Passing Arrays thru url

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
jk2008
Forum Newbie
Posts: 6
Joined: Sun Jan 04, 2009 4:48 pm

Passing Arrays thru url

Post by jk2008 »

I tried to create array and pass it thru url but I am not sure what I did wrong. I am getting 'link[1]' instead of page1.html in the form field. Here is the sample of my code. Can someone please take a look and help me out?


What I am trying to accomplish is to pass the value 'page1.html' to a form thru url. The url is very long and therefore I do not want to include it in the link itself.


Thanks.

1st page with the links.

Code: Select all

<?php  
session_start (); 
 
$link[1] = 'page1.html'; 
$link[2] = 'page2.html'; 
..... 
?> 
 
<body> 
<a href="form.php?name='link[1]'">link 1</a> 
...... 
</body>

form.php

Code: Select all

<?php  
session_start (); 
$link = $_GET['name'];  
?> 
<form action="process.php" method="post"> 
<input name="link" type="text" value="<?php echo $link?>" readonly> 
</form>
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Passing Arrays thru url

Post by watson516 »

jk2008 wrote:

Code: Select all

<?php  
session_start (); 
 
$link[1] = 'page1.html'; 
$link[2] = 'page2.html'; 
..... 
?> 
 
<body> 
<a href="form.php?name='link[1]'">link 1</a> 
...... 
</body>

That should probably be:

Code: Select all

<?php
session_start();
 
$link[1]='page1.html';
...
?>
 
...
<a href="form.php?name=<?php echo $link[1]; ?>">Link 1</a>
...
You echoed the php in the second code sample but not the first.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Passing Arrays thru url

Post by John Cartwright »

or you can serialize() the array, and unserialize() it to get the array back. You should be aware of the limitations in some browsers of the maximum length of the url. Therefore, it's better to POST the data.
Post Reply