tags:

views:

46

answers:

5

Form: $headerValues=array();

$headerValues[1][2]="Test";
... 
....
echo "<input type=\"hidden\" name=\"ArrayData\" value=\"$headerValues\"/>";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />"; 

How do I read headerValues on FORM POST , I see as ARRAY when I use this code

foreach (array_keys($_POST) as $key) { 
   $$key = $_POST[$key]; 
   print "$key is ${$key}<br />";
   print_r(${$key}); 
} 
A: 

You can use:

$headerValues=htmlspecialchars(serialize(array('blah','test')));
echo "<input type=\"hidden\" name=\"ArrayData\" value=\"$headerValues\"/>";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />"; 

to get

$value = unserialize($_POST['ArrayData']);

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

Bang Dao
+3  A: 

The problem is you're outputting the string "ARRAY" as the value of your field. This is what happens when you cast an array to a string in PHP. Check the HTML source next time you have similar problems, it's a pretty crucial step in debugging HTML.

Use this instead:

echo "<input type=\"hidden\" name=\"ArrayData\" value=\"", implode(' ', $headerValues), '"/>';

The way you handle the input is also needlessly complex, this would suffice:

foreach($_POST as $key => $value)
  echo "$key is $value<br />";
meagar
Haha. I like how out of 5 answers you have the only one that's anywhere near the actual problem.
Tim
A: 

You need to write out multiple <input name="ArrayData[]"> elements, one for each value. The empty square brackets signify to PHP that it should store each of these values in an array when the form is submitted.

$headerValues=array('blah','test');

for ($headerValues as $value) {
    echo "<input type=\"hidden\" name=\"ArrayData[]\" value=\"$value\"/>";
}

Then $_POST['ArrayData'] will be an array you can loop over:

foreach ($_POST['ArrayData'] as $i => $value) {
    echo "ArrayData[$i] is $value<br />";
}
John Kugelman
A: 

You are echoing the array flatly here, which is not correct:

echo "<input type=\"hidden\" name=\"ArrayData\" value=\"$headerValues\"/>";

You can either make $headerValues an associative array and using a foreach loop to echo out multiple hidden inputs for each value (which could get messy/bloated depending on how large your array gets):

foreach ($headerValues as $header => $value) {
    printf('<input type="hidden" name="%s" value="%s" />', 
        htmlspecialchars($header, ENT_QUOTES), 
        htmlspecialchars($value, ENT_QUOTES)
    );
}

Or using implode() as meagar suggests, or serialize() and unserialize() as Bang Dao suggests.

BoltClock
A: 
foreach ( $_POST as $name => $value ) {
    echo "Argument: ".$name." Value: ".$value."<br>";
}
JCD