Pass and Get ID from JavaScript to PHP Controller(从 JavaScript 向 PHP 控制器传递和获取 ID)
问题描述
我有一个内联的可编辑表格(我为此使用了
然后我放置了一个 console.log
来查看错误详细信息,我得到了这个:
错误请求(#400):缺少必需的参数:id"
这是我的控制器操作:
公共函数 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->保存(假);} else if ($input['action'] === 'delete') {$assetModel->status = "已删除";$assetModel->保存(假);}回声 json_encode($input);返回 yiihelpersJson::encode(['消息' =>'成功',]);}
我真的不知道如何解决这个问题.如何将 id 传递给控制器?我希望你明白这一点.如果您有任何问题,请告诉我.如果您对实施有其他想法,也请告诉我.
当你把 id 作为 file.assetID 放在代码的开头并使用 file[0].assetID 获取 id
请使用 file.assetID 获取 url 中的 id.
谢谢
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 yiihelpersJson::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.
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
这篇关于从 JavaScript 向 PHP 控制器传递和获取 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 JavaScript 向 PHP 控制器传递和获取 ID
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01