使用jquery,ajax和php prob停用帐户

时间:2018-07-21 17:47:00

标签: javascript php jquery ajax

我正在我正在工作的网站上添加停用功能,因此我添加了一个带有textarea的表单,以告知用户停用其帐户的原因以及在填写textarea时启用的按钮,因此我通过jquery ajax将调用发送到一个php脚本,该脚本会将数据库中的users_table更新为1以便停用,然后必须注销用户并将其重定向到网站的索引页面。因此,一切正常,除非没有注销并且没有重定向。我需要帮助

这是我的php脚本:

    require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/validation_functions.php';

// this to prevent from accessing this file by pasting a link to it
if(!is_ajax_request()) {
    exit;
}

$user_id = (int)$_SESSION["user_id"];

if(isset($_POST['deactivate_reason'])) {
    $deactivate_reason = mysql_prep($_POST['deactivate_reason']);

    // INSERT into table
    $query1  = "INSERT INTO deactivate_reason_table ( ";
    $query1 .= "user_id, reason";
    $query1 .= ") VALUES (";
    $query1 .= " $user_id, '$deactivate_reason'";
    $query1 .= ")";
    $result1 = mysqli_query($connection, $query1);
    $confirm_query1 = confirm_query($result1);

    // if query1 is successful and replies deleted then we make the second query to delete the board comments
    if ($confirm_query1 == 0) {
        echo "error";
        exit();
    } else {
        // UPDATE table
        $query2  = "UPDATE users_table ";
        $query2 .= "SET deactivated = 1";
        $query2 .= "WHERE user_id = $user_id";
        $result2 = mysqli_query($connection, $query2);
        $confirm_query2 = confirm_query($result2);

        if ($confirm_query2 == 0) {
            echo "error";
            exit();
        } else {
            if (isset($_COOKIE['username'])) {
                // setcookie($name, $value, $expiration_time)
                setcookie("username", '', time() - 42000, '/', $_SERVER['SERVER_NAME'] );
            }
            if (isset($_COOKIE['user_id'])) {
                setcookie("user_id", '', time() - 42000, '/', $_SERVER['SERVER_NAME'] );
            }

            session_destroy();
            // redirect_to() is a custom function in the functions.php that redirects
            redirect_to("../index.php");
        }

    }
}

这是我的jquery ajax脚本:

    $(document).on("click", "#deactivate_button", function(e){
    e.preventDefault();

    var text = $("#deactivate_reason_textarea").val();

    var url = "widgets/deactivate.php";

    $.ajax({
        url: url,
        method: "POST",
        data: {
            deactivate_reason: text
        },
        beforeSend: function() {
            CustomSending("Processing...");
        },
        success: function(data){
            $("#deactivate_reason_textarea").val("");
            $("#dialogoverlay").fadeOut("Slow");
            $("#sending_box").fadeOut("Slow");

            $("body").css("overflow", "auto");
        }
    });

});

2 个答案:

答案 0 :(得分:0)

要在PHP中将用户重定向到另一个页面,您可以使用类似MPNowPlayingInfoCentermanual)的方法,但是您正在header('Location: ...')中调用脚本,那么您需要将重定向,而不是调用PHP脚本。

要在ajax中重定向,可以使用JavaScripttutorial)。

window.location

根据您的情况,您需要将其放入 window.location = "my/another/script.php"; 的{​​{1}}中。

success

答案 1 :(得分:0)

好的,非常感谢您的帮助,我设法在您的帮助下解决了这个问题 在像tadeubarbosa建议的那样添加widow.location = "insert.php"之后,我去了index.php页面并添加了以下几行:

    if (logged_in()) {
    $email = $user_data['email'];
    $id = $user_data['user_id'];
    $edited = account_edited($email);
    if ($edited == 0){
        redirect_to("editprofile.php?id=$id");
    }

    $is_deactivated = is_deactivated($_SESSION['user_id']);
    if ($is_deactivated == 1) {
        $query = "UPDATE users_table SET online = 0 WHERE user_id = $id";
        $result = mysqli_query($connection, $query);

        if (isset($_COOKIE['username'])) {
            // setcookie($name, $value, $expiration_time)
            setcookie("username", '', time() - 42000, '/', $_SERVER['SERVER_NAME'] );
        }
        if (isset($_COOKIE['user_id'])) {
            setcookie("user_id", '', time() - 42000, '/', $_SERVER['SERVER_NAME'] );
        }

        session_destroy();
        redirect_to("index.php");
    }
}

然后转到login.php脚本,并添加了一个查询,以更新是否已停用= 0(如果用户登录),因此帐户将重新激活