Page 1 of 1

Problem with passig associative array using form (POST))

Posted: Sat Aug 16, 2003 8:48 am
by szms
I am not sure about how to pass an associative array using form (POST). Here is the code for creating the associative array:

Code: Select all

$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.

Code: Select all

<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>

Posted: Sat Aug 16, 2003 9:54 am
by pootergeist
<input ........." <?= serialize($final_input); ?>" />

$input = unserialize($_POST['formelement']);

var_dump($input);

Posted: Sat Aug 16, 2003 10:20 am
by szms
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.

Code: Select all

$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

Code: Select all

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

var_dump($final_input);

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

?> 

</body> 
</html>

I hope this works for you

Posted: Sat Aug 16, 2003 11:49 am
by derek
$temp1 ="";
if($final_input)
{
$temp1 = implode($final_input, ",");
}

echo $temp1;

Posted: Sat Aug 16, 2003 12:12 pm
by szms
Don't have any clue what you are talking about? Could you please explain me with little more details. Thank you.

Posted: Sat Aug 16, 2003 2:25 pm
by pootergeist
oh - lose the [] from your array definition

either that or serialize($array[])

the [] denotes an extra dimension - so $array[]['field'] = value rather than just $array['field']

Posted: Sat Aug 16, 2003 4:53 pm
by szms
Thank you for your suggestion. I revised my code accoding to your suggestion but I am still unable to print the element of the associative arrray. Could you please take a close look of my code. Thank you.

Code: Select all

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

<form name = "myform" action="test.php" method="POST">
I changed $final_input to $final_input[]

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

Code: Select all

<input type="submit" value="Submit" name="submit"> 
</form>



file: test.php

Code: Select all

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

var_dump($final_input); 

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

?> 

</body> 
</html>

Posted: Sun Aug 17, 2003 4:35 am
by pootergeist
foreach ($final_input as $key => $info)
{
print "$key: $info<br>";
}

think about how many dimensions the array has at every point -

you are putting the first dimension index one of $final_input into the form field - now it becomes single dimension - then you unserialize on output - as you are unserializing a one dimension array you don't need a nested foreach -

i'd still advise losing all cases of [] in your coding - at least until you become proficient in multi-dimension array handling

$final_input = array( .......... no [] here
<input .... serialize($final_input); ..../> no [] here either

$output = unserialize($_POST['formfieldname']); never need [] here anyway

var_dump($output);