Php 一次性保存模型和ajax 2

Php 一次性保存模型和ajax 2,php,ajax,model,yii2,Php,Ajax,Model,Yii2,我的问题是,当我使用Ajax post时,我不知道如何设置$model->code\u reg。我想保存我的模型代码,以便为我的ajax文章循环。但这是我的控制器创建的不是工作保存它 这是我的表格: <div class="form-group"> <?= $form->field($model, 'code_reg')->textInput(['readonly' => true]) ?> </div> <!--I'm us

我的问题是,当我使用Ajax post时,我不知道如何设置
$model->code\u reg
。我想保存我的模型代码,以便为我的ajax文章循环。但这是我的控制器创建的不是工作保存它

这是我的表格:

<div class="form-group">
    <?= $form->field($model, 'code_reg')->textInput(['readonly' => true]) ?> 
</div> <!--I'm using random code and It's was display for this--> 
<table id="sampleTbl", class="table table-striped table-bordered">
    <thead>
        <th>Name</th>
        <th>Age</th> 
    </thead><tbody>
        <tr>
            <td>William</td>
            <td>29</td>
        </tr><tr>
            <td>Nency</td>
            <td>25</td>
        </tr>
    </tbody>
</table>

<div class="form-group">
    <?= Html::submitButton('Create',['class' => 'btn btn-success', 'id' => 'idOfButton']) ?>
</div>
这是我的控制器
actionCreate()

$model=new multiplearray();
if(Yii::$app->request->isAjax){
$tableData=stripcslashes($_POST['pTableData']);
$tableData=json_decode($tableData,true);
foreach($tableData作为$key){
$model->isNewRecord=true;
$model->id=NULL;
$model->name=$key['name'];
$model->age=$key['age'];

$model->code\u reg=$model->code\u reg;//嗯,在
actionCreate
中,您在哪里设置数据到
$model->code\u reg
?否。
$model->code\u reg
它来自表单
activeForm
它来自jQuery。我有数据库
名称、年龄、code\u reg
名称和年龄使用ajax发送,而code\reg>从表单发送
activeForm
请显示您的acrtiveForm..好的,但在实际操作中,您创建了空模型
$model=new multiplearray();
,没有任何数据。它已经在那里
使用jQuery和'table'在其内部activeForm中设置
$('#idOfButton').click(function(){
    var TableData = new Array();
    $('#sampleTbl tr').each(function(row, tr){
        TableData[row]={
            'name' : $(tr).find('td:eq(0)').text(),
            'age' : $(tr).find('td:eq(1)').text()
        }
    }); 
    TableData.shift();  // first row will be empty - so remove
    var jsonEncode = JSON.stringify(TableData);
    // alert(jsonEncode);

    $.ajax({
        type: "POST",
        data: "pTableData=" + jsonEncode,
        success: function(msg){
            // alert(msg);
        },
    });
});
$model = new Mutiplearray();

if(Yii::$app->request->isAjax) {
    $tableData = stripcslashes($_POST['pTableData']);
    $tableData = json_decode($tableData, true);
    foreach ($tableData as $key) {
        $model->isNewRecord = true;
        $model->id = NULL;
        $model->name = $key['name'];
        $model->age = $key['age'];
        $model->code_reg = $model->code_reg; // <---This is my problem I can't save it `code_reg` for ajax looping 
        $model->save();
    }

    return $this->redirect(['index']);
} else {
    return $this->render('create', [
        'model' => $model,
    ]);
}