Javascript 如何将Json以表单形式发布到控制器,然后保存到数据库

Javascript 如何将Json以表单形式发布到控制器,然后保存到数据库,javascript,php,json,controller,yii2,Javascript,Php,Json,Controller,Yii2,我有一个带有示例表值、我的表(id=sampleTbl)HV2列名和年龄的表单。我想在单击submitButton(id=idOfButton)时将其保存到我的数据库表person(id=AI、name、age) 下面是我的源代码,这是我的Javascript代码: <?php $script = <<< JS $('#idOfButton').click(function(){ var myTableArray = []; $("table#sam

我有一个带有示例表值、我的表(id=sampleTbl)HV2列名和年龄的表单。我想在单击submitButton(id=idOfButton)时将其保存到我的数据库表person(id=AI、name、age)

下面是我的源代码,这是我的Javascript代码:

<?php  
$script = <<< JS

$('#idOfButton').click(function(){
    var myTableArray = [];
    $("table#sampleTbl tr").each(function () {
        var arrayOfThisRow = [];
        var tableData = $(this).find('td');
        if (tableData.length > 0) {
            tableData.each(function () {
               arrayOfThisRow.push($(this).text());
            });
            myTableArray.push(arrayOfThisRow);
        }
    });
    var jsonEncode = JSON.stringify(myTableArray);
    // alert(jsonEncode);

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

});

JS;
$this->registerJs($script);
?>
我的表格:

<?php $form = ActiveForm::begin(); ?>

<table id="sampleTbl", class="table table-striped table-bordered">
    <thead>
        <tr id="myRow">
            <th>Name</th>
            <th>Age</th>
        </tr> 
    </thead>
    <tbody>
        <tr>
            <td>william</td>
            <td>32</td>
        </tr>
        <tr>
            <td>Muli</td>
            <td>25</td>
        </tr>
        <tr>
            <td>Sukoco</td>
            <td>29</td>
        </tr>
    </tbody>
</table>

<div class="form-group">
    <?= Html::submitButton('Create',['class' => 'btn btn-success', 'id' => 'idOfButton']) ?>
</div>

<?php ActiveForm::end(); ?>

名称
年龄
威廉
32
穆利
25
苏可可
29
我不知道如何保存所有值数组。当我使用like
$tableData[0]['name']时它只保存第一行。如何保存所有价值

public function actionCreate()
{
    $model = new Person();

    if (Yii::$app->request->isPost) {            
        $post = file_get_contents("php://input");
        $data = JSON::decode($post, true);
        $model->attributes = $data;//Also check this array, this should be right assoc array, and indexes must be contain model attributes names also
        $response = array();
if($model->save()){
 header('Content-type:application/json');
        $response['success'] = "Everything is good";
   return   $this->render('create', [
            'model' => $model,
            'message' => $model,
        ]);      
}else{
      $response['error'] = "Data not valid";
   return   $this->render('create', [
            'model' => $model,
            'message' => $model,
        ]);
}
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    } 
}

你能检查一下吗?如果有什么问题,我们将纠正它们

使用lopping foreach保存数组:

foreach ($tableData as $key) {
    $model->isNewRecord = true;
    $model->id = NULL;
    $model->name = $key['name'];
    $model->age = $key['age'];
    $model->save();
}

你在提交时得到了什么样的响应标题?当我显示警报时,它是这样的“[[william,23],[lia,22],[roy,18]]。然后想保存到数据库但是响应头是什么?打开DevTools并提交。然后带着响应标题回到这里。它在网络下。你必须在退出()之前调用$model->save()方法。验证后也不要检查模型中的错误。确定我认为你的javascript正在工作,但你的控制器没有正确处理,现在我将发布答案now have error
$decode=json\u decode((字符串)$json,$asArray)
case JSON_ERROR_语法:抛出新的InvalidParamException(“语法错误”)你能检查你的视图文件编码吗。编码应该是utf-8我很抱歉再次打扰你,我已经尝试了这个代码。然后我稍微修改一下这个代码,它就成功了。但我有新问题你能帮我吗?我正在更新我的代码和新的ask@NuriddinRashidov
foreach ($tableData as $key) {
    $model->isNewRecord = true;
    $model->id = NULL;
    $model->name = $key['name'];
    $model->age = $key['age'];
    $model->save();
}