user3450036
user3450036

Reputation: 85

php form sending array in input's value field with POST

I have a question about sending data with POST in a form.

I have to send an array(j, i, 59, j, k, 59); like value of a checkbox.

Checkbox with same name should accept multiple values, so i need to send a multidimensional array

array("sunday", "Monday", ...);
echo "<form action ='' method='POST'>";
// some code...
for ($i = 0; $i < count($day); $i++) {
    for ($j = 0; $j < $N; $j++) {
        $value = array($va1, $var2, $var3, ..$varN);
        echo "<input type='checkbox' name='$day" . "[]' value='$value' /> $i-$j";
    }
}
echo "</form>";

var1:varN are integer that depends by i, j and other variables

in $_POST['Monday'] should be array((array(par1,..ParN),(array(par1,..ParN)) but it don't work, because in the receiving side, code like

$Data= $_POST['Monday'] ;

gets (using var_dump):

array[k] (Where k is the number of checked box, and it is ok). Each entry is the string(5)="Array" but i want an array of value, not a string.

Can someone help me? Thank you very much and sorry for my bad english!

Upvotes: 2

Views: 16944

Answers (2)

Jorge Rivera
Jorge Rivera

Reputation: 171

You could just use json_encode($array); the array into the value field and then just json_decode($_POST[$day],true); in case you would want to use more complex arrays.

Upvotes: 2

Marc B
Marc B

Reputation: 360572

Basic PHP: An array used in a string context is the literal word Array:

$foo = array();
echo $foo; // output: "Array"

You have this:

$value=array($va1,$var2,$var3,..$varN);
^^^^^---- $value is an array

echo "<input type='checkbox' name='$day" . "[]' value='$value' /> 
                                                       ^^^^^^----output in string context

If you'd bothered doing a "view source" on your page, you'd have seen the HTML was literally:

<input type='checkbox' name='Monday[]' value='Array' />

You need to implode() that array so it becomes a string:

echo "<input type='checkbox' name='{$day}[]' value='" . implode(',', $value) . "' />";

Upvotes: 3

Related Questions