how to save multi field to "database" in one coulmn

i want to save the array called "answers" to database in one column called "EM_PHONE" with a comma between the date-fields instead of showing it as "alert".

Note: i didn’t add any changes to “Controller” and “Model”

script code:




<script>

    $(document).ready(function(){


    var i = $('input').size() + 1;


    $('#add').click(function() {

    $('<?php echo $form->textField($model, 'EM_PHONE', array('class' => 'field form-group form-control'))?>').fadeIn('slow').appendTo('.inputs');

    i++;

    });


    $('#remove').click(function() {

    if(i > 2) {

    $('.field:last').remove();

    i--;

    }

    });


    $('#reset').click(function() {

    while(i > 2) {

    $('.field:last').remove();

    i--;

    }

    });


    $('.submit').click(function(){


    var answers = [];

    $.each($('.field'), function() {

    answers.push($(this).val());

    });


    if(answers.length == 0) {

    answers = "none";

    }

    

    alert(answers);


    return false;


    });


    });

</script>



[b]

in _form:

[/b]




<div class="form-group col-lg-12 row inputs">

         <?php echo $form->textFieldGroup($model, 'EM_PHONE', array('wrapperHtmlOptions' => array('class' => ''), 'widgetOptions' => array('htmlOptions' => array('placeholder' => 'Type Phone Number to verify.')), 'prepend' => '<i class="glyphicon glyphicon-glass"></i>')); ?>

       </div>

       <div class="form-group">

            <a class="btn btn-xs btn-info" id="add">Add Another</a>

            <a class="btn btn-xs btn-danger" id="remove">Remove</a>

       </div>



please i want an answer asap

I’d choose JSON for such structure instead. The saving itself should not be tricky. Get what’s submitted from Yii::$app->request, iterate it with foreach, create an array from it, then json_encode it and make an SQL query.

I’m using this in server side to transform the array in POST into a string before saving it.




//put this in your model class


    public function rules()

    {

        return [

            ['lojas_ids', 'filter', 'filter' => function ($value) {

                if(!empty($value))

                    return implode(',',$value);

            }], 

        ];

    }