Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to pass an array through post?

I have two pages:

  • Graph.php
  • List.php

The Graph page does exactly what it is named, graphs data. If there is no post/get data it displays all the data in a given table.

The List page is a huge table which loads around 500-600 rows of data. In the table you can sort and filter the rows using JavaScript. The table is around 14 columns wide.

After sorting the rows in the List page you can press a button 'Graph' that will take the visible rows and graph them on the graph page.

What I am having trouble with is passing these ID's over to the graph page. I started with:

<?php
if(isset($_POST['data']))
{
    echo "FOUND SERIALIZED ARRAY<br>";
    $afterSerializeArray = unserialize($_POST['data']);
    print_r($afterSerializeArray);
}
    $beforeSerializeArray = array();
    $beforeSerializeArray[] = 1;
    $beforeSerializeArray[] = 2;
    $beforeSerializeArray[] = 3;
    $serializeArray = serialize($beforeSerializeArray);
?>
<form action="" method="post">
<input type="hidden" name="data" value="<?php echo $serializeArray; ?>"/>
<input type="submit" value="Serialize"/>
</form>

I have written the small snippet to grab the ID's of the visible rows and load them into an array, serialize it and pump it into a variable to post it over to the graph.

Should I be using GET? Should I be doing this a different way?

The reason I wanted the filter and sort on a different page than the graph is because users have a lot of columns and options to filter and sort by.

like image 612
user1627928 Avatar asked Dec 05 '25 13:12

user1627928


1 Answers

Rather than trying to send array over post you should concatenate these ids with any special character (say ','). This way you will get all IDs as comma separated values in $_POST['data']. Now you can use PHP explode function to get all the values in an array and use them as you wish.

This code sample might help you

<?php
if(isset($_POST['data']))
{
    echo "FOUND Ids<br>";
    $IdArray = explode(',',$_POST['data']);
    print_r($IdArray );
}
    $idarray = array('1','2','3');
    $ids = implode(',',$idarray);
    ?>
<form action="" method="post">
<input type="hidden" name="data" value="<?php echo $ids;?>"/>
<input type="submit" value="Serialize"/>
</form>
like image 184
flawless Avatar answered Dec 08 '25 03:12

flawless



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!