dsc862009 2016-03-03 06:45
浏览 40

从JavaScript传递并获取ID到PHP控制器

I have an inline editable table (I used Tabledit for this) and each row has an ID and the IDs should be passed to the controller action (Yii2) in order for me to save edited data to the database. Here's my Tabledit code in my js file:

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);
    }
});

I was expecting that it would point to the url specified (urlID where save-inline-edit is an action function in my controller--public function actionSaveInlineEdit($id){...}) after saving the inline edit, but as I inspect element (after saving), it gives me this error: enter image description here

Then I placed a console.log to view the error details and I get this: enter image description here

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

Here's my controller action:

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',
    ]);
}

I really don't know how to figure this out. How do I pass the id to the controller? I hope you understand this. Please let me know if you have questions. If you have other idea for the implementation, let me know as well.

  • 写回答

1条回答 默认 最新

  • dtxq82489 2016-03-03 06:54
    关注

    As you put the id as file.assetID in start of code and get the id using file[0].assetID

    please use file.assetID to get the id in url.

    Thanks

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决