javascript - 从 JavaScript 传递并获取 ID 到 PHP Controller

标签 javascript php jquery yii frontend

我有一个内联可编辑表(我为此使用了Tabledit),每一行都有一个ID,并且ID应该传递给 Controller ​​操作(Yii2),以便我将编辑后的数据保存到数据库中。这是我的 js 文件中的 Tabledit 代码:

file.assetID = info.response; // the ID

for (var i = 0; i < file.length; i++) { // the table
    if (file[i].type == "image/jpeg") {
        var type = "photo";
    } else if (file[i].type == "video/mp4") {
        var type = "video";
    }

    messageHtml += '<tr id="' + file[i].assetID + '">';
    messageHtml += '<td style="display:none;" id="' + file[i].assetID + '">' + file[i].assetID + '</td>';
    messageHtml += '<td>' + file[i].name + '</td>';
    messageHtml += '<td>' + type + '</td>';
    messageHtml += '<td>' + file[i].size + " KB" + '</td>';
    messageHtml += '<td><input type="text" class="form-control" placeholder="Tag"></td>';
    messageHtml += '<td><input type="text" class="form-control" placeholder="Description"></td>';
    messageHtml += '</tr>';
}

var urlID = "../save-inline-edit/" + file[0].assetID; // url plus the ID of the row
$('#uploader_table').Tabledit({
    url: urlID,
    columns: {
        identifier: [0, 'id'],                    
        editable: [[1, file.name]/*, [3, file.tag], [4, file.description]*/]
    },
    onSuccess: function(data, textStatus, jqXHR) {
        console.log(data);
        console.log(textStatus);
        console.log(jqXHR);
    },
    onFail: function(jqXHR, textStatus, errorThrown) {
        console.log(file.assetID);
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
    }
});

我期望它会指向指定的 url(urlID,其中 save-inline-edit 是我的 Controller 中的一个操作函数 - public 函数actionSaveInlineEdit($id){...}) 保存内联编辑后,但当我检查元素(保存后)时,它给了我这个错误: enter image description here

然后我放置了一个 console.log 来查看错误详细信息,我得到了以下信息: enter image description here

"Bad Request (#400): Missing required parameters: id"

这是我的 Controller 操作:

public function actionSaveInlineEdit($id)
{
    header('Content-Type: application/json');
    $assetModel = $this->findModel($id);

    $input = filter_input_array(INPUT_POST);

    if ($input['action'] === 'edit') {
        $assetModel->title = "";
        $assetModel->description = "";
        $assetModel->save(false);
    } else if ($input['action'] === 'delete') {
        $assetModel->status = "deleted";
        $assetModel->save(false);
    }

    echo json_encode($input);
    return \yii\helpers\Json::encode([
        'message' => 'success',
    ]);
}

我真的不知道如何解决这个问题。如何将 id 传递给 Controller ​​?我希望你明白这一点。如果您有疑问,请告诉我。如果您对实现有其他想法,也请告诉我。

最佳答案

当您将 id 作为 file.assetID 放在代码开头并使用 file[0].assetID 获取 id

请使用file.assetID获取url中的id。

谢谢

关于javascript - 从 JavaScript 传递并获取 ID 到 PHP Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764986/

相关文章:

jquery - scrollTop() 不适用于滚动 :touch

Javascript 事件 : window. 事件与参数引用(函数)

javascript - jQuery:当通知可见时自动隐藏通知

面向 ASP.NET MVC/NHibernate 开发人员的 PHP MVC 框架

javascript - 使用Ajax获取php函数调用的数组

jquery - 从颜色框幻灯片中删除图像编号

jquery - 如何使用 jQuery 过滤表(表数据从 JSON 文件填充)?

javascript - 对 native 方法和属性执行 console.log

javascript - Angular TS 错误 TS1109

php - DirectAdmin 上的 OPcache php5.5 已禁用