Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass array to another page by using anchor in php

Tags:

php

I have to pass an array value from one php page to another php page using <a href> ... here is my coding

$cluster is an array

echo "<td><a href=myebon.php&cluster[]=".$cluster.">Click here to OFF</a></td>";

in myebon.php

$n=count($_GET[cluster]);
for($i=0;$i<=$n;$i++)
{
    echo $cluster[$i]=$_GET['cluster'][$i];
}

The value is not accessible in the second page, it's displaying as array but not the values. I have tried serialization concept too ...

like image 877
velu2143 Avatar asked Dec 12 '22 17:12

velu2143


2 Answers

This is clear example code

first.php

<?php
$Mixed = array("1","2","3");
$Text = json_encode($Mixed);
$RequestText = urlencode($Text);
?>
<a href="second.php?cluster=<?php echo $RequestText; ?>">Click</a>

second.php

<?php
$Text = urldecode($_REQUEST['cluster']);
$Mixed = json_decode($Text);
print_r( $Mixed);
?>

I have checked, and it is working fine.

like image 84
Jerald Avatar answered Dec 14 '22 05:12

Jerald


Use http_build_query

$data = array('foo', 'bar', 'lol');

echo '<a href="myebon.php?' . http_build_query(array('cluster' => $data)) . '">link</a>';

Output

<a href="myebon.php?cluster%5B0%5D=foo&cluster%5B1%5D=bar&cluster%5B2%5D=lol">link</a>

It can be retrieved with $_GET['cluster'], e.g:

foreach ($_GET['cluster'] as $val) { 
    // my work here, example:
    echo $val , "\n";
}
like image 23
Federkun Avatar answered Dec 14 '22 07:12

Federkun