从JavaScript传递和获取ID到PHP控制器 [英] Pass and Get ID from JavaScript to PHP Controller

查看:125
本文介绍了从JavaScript传递和获取ID到PHP控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我希望在保存内联编辑后,它将指向指定的URL(urlID,其中save-inline-edit是控制器中的动作功能-public function actionSaveInlineEdit($id){...}),但是当我检查元素时(保存后) ,它给了我这个错误:

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:

然后我放置了console.log来查看错误详细信息,我得到了:

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

错误的请求(#400):缺少必需的参数:id"

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

这是我的控制器动作:

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传递给控制器​​?希望您能理解.如果您有任何疑问,请告诉我.如果您对实现还有其他想法,请也告诉我.

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.

推荐答案

将id作为file.assetID放在代码开头,然后使用file [0] .assetID

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

请使用file.assetID获取网址中的ID.

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

谢谢

这篇关于从JavaScript传递和获取ID到PHP控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆