Page 1 of 1

Passing Arrays thru url

Posted: Fri Jan 09, 2009 10:18 am
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>

Re: Passing Arrays thru url

Posted: Fri Jan 09, 2009 10:24 am
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.

Re: Passing Arrays thru url

Posted: Fri Jan 09, 2009 12:35 pm
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.