用户离开前Jquery ajax mysql更新

时间:2016-05-30 15:10:30

标签: php jquery mysql ajax

你好我编写了一个脚本来更新用户试图离开页面的信息,但我的代码不能正常工作,不知道为什么我需要帮助来解决这个问题:

jquery:

$(window).bind('beforeunload', function(){

   $.ajax({
        type: 'POST',
        url: 'updateusers.php',
        data: {
            username: username,
            usertype: usertype,
            email: email,
            level: level,
            exp: exp,
            money: money,
            ninjarank: ninjarank,
            avatar: avatar,
            village: village,
            clan: clan,
            guild: guild,
            healthpoint: healthpoint,
            chakrapoint: chakrapoint,
            ninjutsu: ninjutsu,
            taijutsu: taijutsu,
            genjutsu: genjutsu,
            senjutsu: senjutsu,
            talent: talent,
            chakra: chakra,
            health: health,
            chakracontrol: chakracontrol
        }
    });
});

updateusers.php:

<?php
require "mysql.php";
session_start();

    $userid = $_POST["userid"];
    $usertyp = $_POST["usertype"];
    $email = $_POST["email"];
    $level = $_POST["level"];
    $exp = $_POST["exp"];
    $money = $_POST["money"] + 1000;
    $ninjarank = $_POST["ninjarank"];
    $avatar = $_POST["avatar"];
    $village = $_POST["village"];
    $clan = $_POST["clan"];
    $guild = $_POST["guild"];
    $healthpoint = $_POST["healthpoint"];
    $chakrapoint = $_POST["chakrapoint"];
    $ninjutsu = $_POST["ninjutsu"];
    $taijutsu = $_POST["taijutsu"];
    $genjutsu =  $_POST["genjutsu"];
    $senjutsu = $_POST["senjutsu"];
    $talent = $_POST["talent"];
    $chakra = $_POST["chakra"];
    $health = $_POST["health"];
    $chakracontrol = $_POST["chakracontrol"];

        $stmt = $connect->prepare("UPDATE users u SET 
        u.usertype=?,
        u.email=?,
        u.level=?,
        u.exp=?,
        u.money=?,
        u.ninjarank=?,
        u.avatar=?,
        u.village=?,
        u.clan=?,
        u.guild=?,
        u.healthpoint=?,
        u.chakrapoint=?,
        u.ninjutsu=?,
        u.taijutsu=?,
        u.genjutsu=?,
        u.senjutsu=?,
        u.talent=?,
        u.chakra=?,
        u.health=?,
        u.chakracontrol=?
         WHERE userid= ?");
        $stmt->bind_param("isiiisssssiiiiiiiiiii",
                     $usertype,
                     $email,
                     $level,
                     $exp,
                     $money,
                     $ninjarank,
                     $avatar,
                     $village,
                     $clan,
                     $guild,
                     $healthpoint,
                     $chakrapoint,
                     $ninjutsu,
                     $taijutsu,
                     $genjutsu,
                     $senjutsu,
                     $talent,
                     $chakra,
                     $health,
                     $chakracontrol,
                     $userid
                         );

        $stmt->execute();
        $stmt->close();
        $connect->close();
?>

所以我尝试发送用户试图将页面留给updateusers.php的所有信息,并在那里进行更新查询但由于某种原因它不能正常工作,我这样做只是为了看它是否有效$ money = $ _POST [&#34;钱&#34;] + 1000;我每次用户离开时都会添加+1000但在数据库中没有任何变化

2 个答案:

答案 0 :(得分:0)

首先,您必须使用print_r($ _ POST)检查updateusers.php上是否存在所有帖子值。

然后你会在这里看到哪些值是发布的,哪些不是。之后应用您的更新条件。

答案 1 :(得分:0)

使用 beforeunload 事件,您必须返回一条消息,该消息将提醒用户。有关更多详细信息,请参阅此处: https://developer.mozilla.org/en/docs/Web/Events/beforeunload

在你的情况下:  1.确保ajax代码没有错误。  2.在事件函数中添加返回值。

$(window).bind('beforeunload', function(){
   var confirmationMessage = 'Are you sure you want to leave?';
   $.ajax({
        type: 'POST',
        url: 'updateusers.php',
        data: {
            username: username,
            usertype: usertype,
            email: email,
            level: level,
            exp: exp,
            money: money,
            ninjarank: ninjarank,
            avatar: avatar,
            village: village,
            clan: clan,
            guild: guild,
            healthpoint: healthpoint,
            chakrapoint: chakrapoint,
            ninjutsu: ninjutsu,
            taijutsu: taijutsu,
            genjutsu: genjutsu,
            senjutsu: senjutsu,
            talent: talent,
            chakra: chakra,
            health: health,
            chakracontrol: chakracontrol
        }
    });
    e.returnValue = confirmationMessage;// Gecko, Trident, Chrome 34+
    return confirmationMessage;// Gecko, WebKit, Chrome <34
});

我希望有所帮助。