dongou4052 2015-09-21 19:16
浏览 78

在JavaScript中读取PHP会话变量

I want to send the value of my PHP session variable to JavaScript in the same file. I tried this code, but it doesn't work. Please help me resolve the issue.

Here is what I am trying :

<?php
session_start();
?>

<h2>
<?php
echo "Welcome, " .$_SESSION["name"];
?>
</h2>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="chat.js"></script>
</head>
<body onload="init();">
<noscript>
Your browser does not support Javascript!!
</noscript>

<!-- Some HTML Code -->

<div>
    <a href="../index.php">Go back</a>
    <a href="../home.php?SignOut" id= "left">Sign Out</a>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $("#left").click(function() {
                //remove user_name. Set action: left
                var user_name = <?php echo json_encode($_SESSION["name"]) ?>;
                $.post('php/users.php', {user_name: user_name, action: 'left' });

            });
});
</script>
</div>  

</body>
</html>

This is my users.php file

if(isset($_POST['user_name'], $_POST['action'])) {
    $user_name = $_POST['user_name'];
    $action = $_POST['action'];

    if($action == 'joined') {
        user_joined($user_name);
    }
}


else if(isset($_POST['action'])) {
    $action = $_POST['action'];

    if($action == 'list') {
        foreach(user_list() as $user) {
            $link_address = "Chat/index.php";
            echo '<a class="a" name="a" href='.$link_address.'>'.$user.'</a>';
            echo '<br />';
        }
    }
    else if($action == 'left') {
        //call user_left function
        user_left();
    }

}

function user_left() {
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

    //Create connection

    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $user_name = $_SESSION["name"];


    $sql = "DELETE FROM online_users WHERE user_name = '$user_name'";
    $query = "DELETE FROM chat WHERE to_user = '$user_name'";

    $result = $conn->query($query);

    if($conn->query($sql) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error in deleting: " . $sql. "<br>" . $conn->error;
    }


    $conn->close();
}
  • 写回答

3条回答 默认 最新

  • duanqu9292 2015-09-21 19:36
    关注

    First of all you have to look in debug console for errors (usualy it can be opened with F12 key). I see a problem in your code: php echo statement has to be in outer quotes, because it is interpreted as a string in JS:

    var user_name = "<?php echo $_SESSION["name"] ?>";
    

    Just open console and you will see exact line and character where the error is.

    Other way of passing variables from PHP to JS is cookies.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类