0

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?

6
  • not sure if this will work, but worth a try, <?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?> Commented May 21, 2014 at 13:21
  • Oh no sorry, that is for html options Commented May 21, 2014 at 13:27
  • no. It doesnt send arrays to controller. Hiddenfield only accepts an string, like value in your example Commented May 21, 2014 at 13:27
  • I am starting to see your problem now. I guess you could use javascript to pass the array's as ajax to the controller, save the data as a session variable, and then when you move on to the next action you can access them from the controller again? Commented May 21, 2014 at 13:32
  • Or alternatively, if this is the same controller that you used to render this view, just save the data at this time as a session variable, then when you come back you can access again. Commented May 21, 2014 at 13:33

2 Answers 2

2

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

}
Sign up to request clarification or add additional context in comments.

Comments

1

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']);

3 Comments

thank you for the detail. When I use json_encode, like this <?php $arrayFather = array('1'=>'6'); ?> <input type="hidden" name="father" value="<?php json_encode($arrayFather); ?>"> ... then i look in server, and father is a blank string. I missing something?
yes :) a little small thing - you do not echo the value - change it into: value="<?php echo json_encode($arrayFather); ?>">
WONDERFULl this solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.