douhao2721 2014-01-08 22:06
浏览 87

jQuery ajax总是在Wordpress上失败

I have the script below that erases a line on a MySQL database with ajax. When the function is fired the line gets deleted from the database but I am always alerted "FAILED".

function deleteRow(data){
if(confirm("Are you sure that you wish to remove this entry?
This 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");
        });
}
}

This is 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();
}
  • 写回答

1条回答 默认 最新

  • duanbu1421 2014-01-08 22:15
    关注

    Change the PHP and return 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);
    
    ?>
    

    and

    .done(function(data) {
         if (data.success == 'true') {
            alert('you did it');
         }
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码