Pass and Get ID from JavaScript to PHP Controller

 ̄綄美尐妖づ 提交于 2019-12-13 05:34:29

问题


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:

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

"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:


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



来源:https://stackoverflow.com/questions/35764986/pass-and-get-id-from-javascript-to-php-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!