Q:

JQuery , ajax ,mysql update when user logout

Hello i have coded a script to update information ones user tries to logout but my code are not working and do not know why i need help to solve this :
jQuery : 
$('#logout').click(function() {
    $.ajax({
        type: 'POST',
        url: 'updateusers.php',
        async: 'false'
    });
    alert(username);
});

so i am not sending any data with ajax because i call session in updateusers.php and insert session values , and test this code with alert(username); i got alerted ones i click on log out 
this is my updateusers.php :
<?php
require "mysql.php";
session_start();

$userid = $_SESSION["userid"];
$usertyp = $_SESSION["usertype"];
$email = $_SESSION["email"];
$level = $_SESSION["level"];
$exp = $_SESSION["exp"];
$money = $_SESSION["money"] + 1000;
$ninjarank = $_SESSION["ninjarank"];
$avatar = $_SESSION["avatar"];
$village = $_SESSION["village"];
$clan = $_SESSION["clan"];
$guild = $_SESSION["guild"];
$healthpoint = $_SESSION["healthpoint"];
$chakrapoint = $_SESSION["chakrapoint"];
$ninjutsu = $_SESSION["ninjutsu"];
$taijutsu = $_SESSION["taijutsu"];
$genjutsu = $_SESSION["genjutsu"];
$senjutsu = $_SESSION["senjutsu"];
$talent = $_SESSION["talent"];
$chakra = $_SESSION["chakra"];
$health = $_SESSION["health"];
$chakracontrol = $_SESSION["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();
?>

so to test this it i am adding +1000 to $money each time when users log out and updating database but nothing happens , i tried do a simple insert and update function with a one column but that didt work ether so i am ges that the problem is with ajax function , i have tried to send a value trough him but same thing got no result 

A:

It is Ideal to suggest that you extract your $_SESSION Data into an Array, encode it into JSON and then set it as a Javascript Global Variable, so that it is accessible to your JS Script. Here's how:
PHP: THE LOG OUT PAGE
        // FILENAME: logout.php     // THE LOGOUT PAGE HAS ALL THE $_SESSION DATA SO YOU COULD JUST EXTRACT IT AND PASS IT TO JS:

        // CREATE AN ARRAY TO HOLD YOUR $_SESSION DATA TO BE PASSED TO JAVASCRIPT;          
        $arrSessionData = array(        
            'userid'        => $_SESSION["userid"],
            'usertype'      => $_SESSION["usertype"],
            'email'         => $_SESSION["email"],
            'level'         => $_SESSION["level"],
            'exp'           => $_SESSION["exp"],
            'money'         => intval($_SESSION["money"]) + 1000,
            'ninjarank'     => $_SESSION["ninjarank"],
            'avatar'        => $_SESSION["avatar"],
            'village'       => $_SESSION["village"],
            'clan'          => $_SESSION["clan"],
            'guild'         => $_SESSION["guild"],
            'healthpoint'   => $_SESSION["healthpoint"],
            'chakrapoint'   => $_SESSION["chakrapoint"],
            'ninjutsu'      => $_SESSION["ninjutsu"],
            'taijutsu'      => $_SESSION["taijutsu"],
            'genjutsu'      => $_SESSION["genjutsu"],
            'senjutsu'      => $_SESSION["senjutsu"],
            'talent'        => $_SESSION["talent"],
            'chakra'        => $_SESSION["chakra"],
            'health'        => $_SESSION["health"],
            'chakracontrol' => $_SESSION["chakracontrol"],

        );

        // NOW CREATE A GLOBAL JS OBJECT THAT WOULD BE ACCESSIBLE TO YOUR JAVASCRIPT FILE:
        $SCRIPT_OUT     = '<script type="text/javascript">' . PHP_EOL;
        $SCRIPT_OUT    .= 'var _SESSION_DATA = ' . json_encode($arrSessionData ) . ";";
        $SCRIPT_OUT    .= '</script>' . PHP_EOL;

        // ECHO OUT THE SCRIPT SO THAT THE VARIABLE: _SESSION_DATA IS NOW GLOBAL:
        echo $SCRIPT_OUT;

    ?> 

JAVSCRIPT: THE AJAX SECTION
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script type="text/javascript">
        (function ($) {
            $(document).ready(function (e) {
                $('#logout').click(function() {
                    // PREVENT DEFAULT BEHAVIOUR TILL YOUR AJAX PROCESSING IS DONE.
                    // HOWEVER, EVEN IF YOU DON'T PREVENT THE DEFAULT, YOUR SCRIPT WILL WORK JUST FINE BUT....
                    // IN YOUR CASE YOU DON'T NEED TO BECAUSE ALL YOU WANT IS UPDATE THE USER DATA USING THE SESSION DATA
                    // AND NORMALLY LOG THE USER OUT WITHOUT ANY DRAMA...
                    // UNCOMMENT THE LINE BELOW IF YOU STILL NEED IT...
                    /* e.preventDefault();   */ 
                    $.ajax({
                        type: 'POST',
                        url: 'updateusers.php',
                        data: {'jsonData': _SESSION_DATA},
                        async: 'false',

                        success: function (data, textStatus, jqXHR){
                            // YOU CAN NOW PROCEED WITH COMPLETE LOGOUT OF THE USER OR WHAT EVER.
                        }
                    });

                    alert(username);
                });
            });
        })(jQuery);

    </script>

PHP: UPDATE_USERS.PHP FILE
    <?php
        // FILENAME: updateusers.php
        require "mysql.php";
        // session_start();      // <== NO NEED FOR SESSION; YOU HAVE THE DATA YOU NEED...

        // GET THE  DATA (ARRAY) FROM AJAX
        $sessionData        = isset($_POST['jsonData']) ? $_POST['jsonData'] : null;

        // IF THE DATA IS NOT NULL, CONTINUE WITH YOUR UPDATE LOGIC....
        if(!is_null($sessionData)){

            // EXTRACT THE DATA AND ALSO DO YOUR UPDATE WITHIN THIS IF BLOCK....
            $userid         = $sessionData["userid"];
            $usertyp        = $sessionData["usertype"];
            $email          = $sessionData["email"];
            $level          = $sessionData["level"];
            $exp            = $sessionData["exp"];
            $money          = intval($sessionData["money"]) + 1000;
            $ninjarank      = $sessionData["ninjarank"];
            $avatar         = $sessionData["avatar"];
            $village        = $sessionData["village"];
            $clan           = $sessionData["clan"];
            $guild          = $sessionData["guild"];
            $healthpoint    = $sessionData["healthpoint"];
            $chakrapoint    = $sessionData["chakrapoint"];
            $ninjutsu       = $sessionData["ninjutsu"];
            $taijutsu       = $sessionData["taijutsu"];
            $genjutsu       = $sessionData["genjutsu"];
            $senjutsu       = $sessionData["senjutsu"];
            $talent         = $sessionData["talent"];
            $chakra         = $sessionData["chakra"];
            $health         = $sessionData["health"];
            $chakracontrol  = $sessionData["chakracontrol"];

            // UPDATE THE USER'S DATA BASED ON THE VALUES ABOVE...
            $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();

        }
    ?>

I hope this helps a little....

