Reputation: 1955
I have one question with two different type of data.
I have a view in Yii, which has got a form control. I want to send an array, and an array of array to the controller, to my create action.
The array is : $arraySons = ('albert','francis','rupert');
The array of array is $arrayFather = ('1'=>array(6,7,8));
I must use some control, so the form will post it in $_POST
?... or this can't be done and I must use JavaScript?
Upvotes: 0
Views: 2433
Reputation: 3103
You can create your form as in the answer of @crafter. I just wirte more details:
<input type="hidden" name="sons[]" value="albert">
<input type="hidden" name="sons[]" value="rupert">
and so on
and then for the fathers you would do something similar:
<input type="hidden" name="father[1][]" value="6">
<input type="hidden" name="father[1][]" value="7">
<input type="hidden" name="father[1][]" value="8">
But if the user does not need to see the data, you could maybe prepare a JSON object with the data, and post it in 1 field, which seem much easier for me
<input type="hidden" name="father" value="<?= json_encode($arrayFather); ?>">
<input type="hidden" name="sons" value="<?= json_encode($arraySons); ?>">
and then in your action you can get the data from post and decode it with json_decode
$myArrayFather = json_decode($_POST['father']);
$myArraySons = json_decode($_POST['sons']);
Upvotes: 1
Reputation: 6296
Normally, in HTML forms you can create an array by having more than one field with the same name, and a array notation.
<input name="sons[]">
<input name="sons[]">
When you submit the form $_POST['sons'] will be an array, and can be handled as follows :
foreach ($_POST['sons'] as $son) {
echo 'Son of the father is '.$son."\n";
}
Upvotes: 2