[英]jQuery ajax done always fails on Wordpress
我下面的腳本使用ajax刪除了MySQL數據庫上的一行。 觸發該功能后,該行將從數據庫中刪除,但始終會收到“失敗”的提示。
function deleteRow(data){
if(confirm("Are you sure that you wish to remove this entry?\nThis cannot be undone")){
$.ajax({
type: "POST",
url: "/wp-content/themes/Rexmed/deleterow.php",
data: {id: data},
dataType: "json"
}).done(function() {
alert("Success");
}).fail(function() {
alert("FAILED");
});
}
}
這是deleterow.php
<?php
require('../../../wp-blog-header.php');
if(!is_user_logged_in()){
auth_redirect();
die();
}
require ('../../../wp-config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$id = $_POST['id'];
if ($stmt = $mysqli->prepare("DELETE FROM customers WHERE id=?")) {
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->close();
}
更改PHP並返回JSON
<?php
require('../../../wp-blog-header.php');
if(!is_user_logged_in()){
auth_redirect();
die();
}
require ('../../../wp-config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$id = $_POST['id'];
if ($stmt = $mysqli->prepare("DELETE FROM customers WHERE id=?")) {
$stmt->bind_param("s", $id);
if ($stmt->execute()) {
$arr = array('success' => 'true');
}else{
$arr = array('success' => 'false');
}
$stmt->close();
}
echo json_encode($arr);
?>
和
.done(function(data) {
if (data.success == 'true') {
alert('you did it');
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.