Jump to content

help


tauchai83

Recommended Posts

how to pass the array to next page?

I have been in trouble in passing array to next page.

The array i use include foreach statement as following.

if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}

I would like to have the items  selected by user pass along to next page
Link to comment
https://forums.phpfreaks.com/topic/34085-help/
Share on other sites

You could pass it in the session (as above), or if you really want to you can pass it from the page as a form value. This is probably not the best way to handle passing an array, but it works.

Try this example:

[code]
<?php
// Create your array
$myarray = array('test','test2','test3');

// encode the array and place it into your form
$myarray = base64_encode(serialize($myarray));

// post the form and get the array back from the page
$newarray = unserialize(base64_decode($myarray));

// echo the output
for($i=0;$i<3;$i++) {
echo $newarray[$i].'<br>';
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/34085-help/#findComment-160239
Share on other sites

so in my case how the coding should look like? can anyone modify for me? i donno the way to pass array...normal value i know using <input type="hidden" value="$xxx">.. how about for array that the element is unknown? as the case above..php newbies here..hehe
Link to comment
https://forums.phpfreaks.com/topic/34085-help/#findComment-160240
Share on other sites

Create these 2 pages:

page1.php

[code]<?php
// Create your array
$myarray = array('test','test2','test3');

// Put this into your form
$myarray = base64_encode(serialize($myarray));
?>
<form method="post" action="page2.php">
<input type="text" name="myarray" value="<?php echo $myarray; ?>">
  <input type="submit" />
</form>[/code]

page2.php

[code]<?php
// get the array back from the form
$newarray = unserialize(base64_decode($_REQUEST['myarray']));

// echo the output
for($i=0;$i<3;$i++) {
echo $newarray[$i].'<br>';
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/34085-help/#findComment-160258
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.