Problem with passig associative array using form (POST))

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
szms
Forum Contributor
Posts: 101
Joined: Thu Jun 26, 2003 12:23 pm

Problem with passig associative array using form (POST))

Post 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>
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

<input ........." <?= serialize($final_input); ?>" />

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

var_dump($input);
szms
Forum Contributor
Posts: 101
Joined: Thu Jun 26, 2003 12:23 pm

Post 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>
derek
Forum Newbie
Posts: 17
Joined: Sat Aug 16, 2003 11:31 am

I hope this works for you

Post by derek »

$temp1 ="";
if($final_input)
{
$temp1 = implode($final_input, ",");
}

echo $temp1;
szms
Forum Contributor
Posts: 101
Joined: Thu Jun 26, 2003 12:23 pm

Post by szms »

Don't have any clue what you are talking about? Could you please explain me with little more details. Thank you.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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']
szms
Forum Contributor
Posts: 101
Joined: Thu Jun 26, 2003 12:23 pm

Post 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>
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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);
Post Reply