Search code examples
phparrayspostyii

YII send array or array of array from view to controller action via form post


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?


Solution

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