For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2004 > passing array in the url









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author passing array in the url
Vess

2004-05-19, 5:31 pm

Hello there,

I am unable to pass an array thought he url. Even the most simple array as
in the code:

$test = array(1,2,3,4);
echo "<a href=receive_array.php?testvar=".$test.">Test</a>";

where receive_array.php is simply:

<?php error_reporting(E_ALL);
$testvar = $_GET['testvar'];

print_r($testvar);

?>

This does not generate any error or warning messages but will simply print
"Array" in the browser. Does this have to do with my php configuration or
do i have to do something special in the code?

Thanks
Vess
Andy Hassall

2004-05-19, 5:31 pm

On Wed, 19 May 2004 22:47:02 +0200, Vess <vessper@itu.dk> wrote:

>Hello there,
>
>I am unable to pass an array thought he url. Even the most simple array as
>in the code:
>
>$test = array(1,2,3,4);
>echo "<a href=receive_array.php?testvar=".$test.">Test</a>";
>
>where receive_array.php is simply:
>
><?php error_reporting(E_ALL);
> $testvar = $_GET['testvar'];
>
> print_r($testvar);
>
>?>
>
>This does not generate any error or warning messages but will simply print
>"Array" in the browser. Does this have to do with my php configuration or
>do i have to do something special in the code?


The string representation of an array is the string "Array". This is clearly
not what you want. You need to construct a URL of the form:

receive_array.php?testvar[]=1&testvar[]=2&testvar[]=3&testvar[]=4

So something like:

$test = array(1,2,3,4);
foreach($test as $val)
$testValues[] = "testvar[]=$val";

echo "<a href='receive_array.php?testvar=";
echo join("&", $testValues);
echo "'>Test</a>";

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
iuz

2004-05-19, 6:30 pm

Vess wrote:

> Hello there,
>
> I am unable to pass an array thought he url. Even the most simple array as
> in the code:
>
> $test = array(1,2,3,4);
> echo "<a href=receive_array.php?testvar=".$test.">Test</a>";


while(list($key, $value) = each($test_var)) $el_var .=
"&test_var[$key]=$value";
$el_var = substr($el_var, 1);

echo "<a href='$PHP_SELF?$el_var'>test</a>

will output::
127.0.0.1?test_var[0]=56&test_var[1]=qwerty&test_var[2]=test

don't forget the ' after and fore the href or with string with space you
will probably have some problem

let me informed

--
www.iuz-lab.info
iuz

2004-05-19, 6:30 pm

while(list($key, $value) = each($test_var)) $el_var .=
"&test_var[$key]=$value";
$el_var = substr($el_var, 1);

echo "<a href='$PHP_SELF?$el_var'>test</a>";

sorry, i forgotten to close the echo..
PATRICK CROCKER

2004-05-19, 9:30 pm

You cannot pass an array through a url in it's raw form. You have to
serialize it, encode it, then on the receiving page you unencode it, and
finally unserialize it.

Example:
<?
$test = array(1,2,3,4);
$serialized = rawurlencode(serialize($test));
echo "<a href=receive_array.php?testvar=".$test.">Test</a>";
?>

then in recieve_array.php:
<?
$testvar = unserialize(rawurldecode($_GET['testvar']));
echo "<pre>";
print_r($testvar);
echo "</pre>";
?>

- Patrick.

"Vess" <vessper@itu.dk> wrote in message
news:pan.2004.05.19.20.47.00.907337@itu.dk...
> Hello there,
>
> I am unable to pass an array thought he url. Even the most simple array as
> in the code:
>
> $test = array(1,2,3,4);
> echo "<a href=receive_array.php?testvar=".$test.">Test</a>";
>
> where receive_array.php is simply:
>
> <?php error_reporting(E_ALL);
> $testvar = $_GET['testvar'];
>
> print_r($testvar);
>
> ?>
>
> This does not generate any error or warning messages but will simply print
> "Array" in the browser. Does this have to do with my php configuration or
> do i have to do something special in the code?
>
> Thanks
> Vess



Patrick Crocker

2004-05-19, 9:30 pm

correction to the example:
echo "<a href=receive_array.php?testvar=".$serialized.">Test</a>";


"PATRICK CROCKER" <patrick.crocker@verizon.net> wrote in message
news:09Tqc.1397$x25.827@nwrddc02.gnilink.net...
> You cannot pass an array through a url in it's raw form. You have to
> serialize it, encode it, then on the receiving page you unencode it, and
> finally unserialize it.
>
> Example:
> <?
> $test = array(1,2,3,4);
> $serialized = rawurlencode(serialize($test));
> echo "<a href=receive_array.php?testvar=".$test.">Test</a>";
> ?>
>
> then in recieve_array.php:
> <?
> $testvar = unserialize(rawurldecode($_GET['testvar']));
> echo "<pre>";
> print_r($testvar);
> echo "</pre>";
> ?>
>
> - Patrick.
>
> "Vess" <vessper@itu.dk> wrote in message
> news:pan.2004.05.19.20.47.00.907337@itu.dk...
as[color=darkred]
print[color=darkred]
or[color=darkred]
>
>



Alberto

2004-05-19, 10:30 pm

Doh! Very well said! That seems really the "right" answer.

Though, to make things more difficult, let's hope no value in the array
includes things like &foo=foo2
Or do you think that won't affect?

PS By the way why does not using post? I am just trying to learn more on
Php!
Alberto
http://www.unitedscripters.com/


"PATRICK CROCKER" <patrick.crocker@verizon.net> ha scritto nel messaggio
news:09Tqc.1397$x25.827@nwrddc02.gnilink.net...
> You cannot pass an array through a url in it's raw form. You have to
> serialize it, encode it, then on the receiving page you unencode it, and
> finally unserialize it.



Vess

2004-05-20, 5:30 am

On Wed, 19 May 2004 22:47:02 +0200, Vess wrote:

> Hello there,
>
> I am unable to pass an array thought he url. Even the most simple array as
> in the code:
> ......


Thanks guys,

I guess I always have to pass the array by breaking it down into
components. Do you know if there is a mothod of passing an array as it is
because I will ba working with a multidimensional array later on?

Anyway I decided that it would be easier just to use implode() on hte
array and then recreate it with preg_split() in ht receiving file.

Thanks again:)
Vess
Patrick Crocker

2004-05-20, 9:30 am

The rawurlencode() function will encode the serialized array making it safe
to be put in the GET query string.

The reason the $_POST array was not reference in the example is because the
data was not originating from an HTML Form

$_GET examples:
<a href='whatever.php?thisvar=thisvalue'>Link</a>
or...
<form action='whatever.php' method='GET'>
<input type='HIDDEN' name='thisvar' value='thisvalue' />
</form>

$_POST example:
<form action='whatever.php' method="POST">
<input type='HIDDEN' name='thisvar' value='thisvalue' />
</form>

- Patrick.

"Alberto" <NOSPAM@hotmail.com> wrote in message
news:c8h11m$3ud$1@lacerta.tiscalinet.it...
> Doh! Very well said! That seems really the "right" answer.
>
> Though, to make things more difficult, let's hope no value in the array
> includes things like &foo=foo2
> Or do you think that won't affect?
>
> PS By the way why does not using post? I am just trying to learn more on
> Php!
> Alberto
> http://www.unitedscripters.com/
>
>
> "PATRICK CROCKER" <patrick.crocker@verizon.net> ha scritto nel messaggio
> news:09Tqc.1397$x25.827@nwrddc02.gnilink.net...
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com