Click to See Complete Forum and Search --> : Problem with passig associative array using form (POST)


szms
08-16-2003, 11:22 AM
I am not sure about how to pass an associative array using form (POST). Here is the code for creating the associative array:




$final_input[] = array(
"term" => "$term",
"position" => "$position",
"menu" => "Function",
"component" => "$store_name"
);

<form name = "myform" action="test.php" method="POST">
<input type="hidden" value= "<?php echo $final_input;?>" name ="final_input">
<input type="submit" value="Submit" name="submit">
</form>




File : test.php; where I am trying to print the element of the array. But getting error. Any suggestion? Thank you.




<html>
<head>
<html>
<head>
</head>
<body>
<?php
$final_input = $_POST['final_input'];

foreach ($final_input as $input)
{
foreach ($input as $key => $info)
{
print "$key: $info<br>";
}
}
?>

</body>
</html>

Jeff Mott
08-16-2003, 02:10 PM
I believe the following function will do what you need.

http://us4.php.net/manual/en/function.serialize.php

szms
08-16-2003, 02:13 PM
Thank you for your suggestion. I revised my code accoding to your suggestion but still I am not able to print the element of the associative arrray.





$final_input[] = array(
"term" => "$term",
"position" => "$position",
"menu" => "Function",
"component" => "$store_name"
);

<form name = "myform" action="test.php" method="POST">
<input type="hidden" value= "<?php echo serialize($final_input); ?>" name ="final_input">
<input type="submit" value="Submit" name="submit">
</form>




file: test.php




<html>
<head>
<html>
<head>
</head>
<body>
<?php
$final_input = unserialize($_POST['final_input']);

foreach ($final_input as $input)
{
foreach ($input as $key => $info)
{
print "$key: $info<br>";
}
}

?>

</body>
</html>

Kr|Z
08-17-2003, 10:16 AM
Try to change to this:


<input type="hidden" value= "<?php echo htmlspecialchars(serialize($final_input)); ?>" name ="final_input">


<html>
<head>
<html>
<head>
</head>
<body>
<?php
$final_input = stripslashes(unserialize($_POST['final_input']));

foreach ($final_input as $input)
{
foreach ($input as $key => $info)
{
print "$key: $info<br>";
}
}

?>

</body>
</html>