[英]POST value not passing to PHP a deleting SQL row
我試圖從我的html表中發布“ ID”,以在單擊刪除按鈕時從SQL數據庫中刪除該行。 到目前為止,似乎POST無法正常工作,但PHP代碼可以正常工作。
這是JavaScript代碼,當我選擇它時,它會從第一列(ID)中獲取所選表行的值,並在按下Delete按鈕時顯示一個消息框以顯示ID(此方法工作正常,因此似乎正在讀取該值) 。 接下來的部分將顯示一個消息框,確認刪除,單擊“確定”將不執行任何操作。
的JavaScript
$(function(){
$("#tblAssets tr").click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
$('#btnDelete').click(function(e){
var value = $("#tblAssets tr.selected td:first").html();
alert(value);
if (confirm("Are you sure you want to delete this asset?")) {
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : value;
success : function() {
}
});
}
});
});
這是PHP代碼。 當我手動調整$ value使其等於數據庫表中的ID時,它會工作並刪除該行。
$value = $_POST['value'];
$sql = "DELETE FROM [Assets].[dbo].[Hardware] WHERE [ID] = $value";
$result = sqlsrv_query( $conn, $sql);
謝謝
這條線是錯誤的
data : value;
嘗試這個
data : {value:value},
^ not semi colon
您正在傳遞沒有參數的值。 添加參數:
$(function(){
$("#tblAssets tr").click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
$('#btnDelete').click(function(e){
var data = {
"value": $("#tblAssets tr.selected td:first").html();
};
if (confirm("Are you sure you want to delete this asset?")) {
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : $.param(data);
success : function() {
}
});
}
});
});
或您的Ajax數據應如下所示:
data: "value="+$("#tblAssets tr.selected td:first").html();
而且你不能使用;
這里:
data : value;
它應該是,
數據:“值” +值,//通過名稱傳遞值:即value = 10。 單獨替換ajax部分,如下所示
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : "value"+value,//
success : function() {
}
});
您沒有創建對象,而是直接動態傳遞了變量。
var value = $("#tblAssets tr.selected td:first").html();
//alert(value);
if (confirm("Are you sure you want to delete this asset?")) {
var dataValue = {
theValue : value // You have to create an object here
};
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : dataValue, // pass the object here
success : function() {
}
});
}
在服務器端:
$value = $_POST['theValue'];
試試這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.