PHP会话变量回显不正确

时间:2017-06-17 14:32:58

标签: php html ajax

我有以下代码:

<?php
    session_start();
?>
<html>
<head>
    <title>Dashboard</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
    <button id="openjob">View open job requests</button>
    <button id="jobtoday">View all job requests today</button>
    <div id="responsecontainer"></div>

    <script type="text/javascript">
    $('#openjob').click(function() {

    <?php  
        $_SESSION["flag"] = 0;
    ?>

    $.ajax({
    type: "GET",
    url: "cssdashsubmit.php",             
    dataType: "html",   //expect html to be returned                
    success: function(response){                    
        $("#responsecontainer").html(response); 
       }
    });    

    });

    $('#jobtoday').click(function() {

    <?php  
        $_SESSION['flag'] = 1;
    ?>

    $.ajax({
    type: "GET",
    url: "cssdashsubmit.php",             
    dataType: "html",   //expect html to be returned                
    success: function(response){                    
        $("#responsecontainer").html(response); 
    }
    });    

    });
    </script>
</body>
</html>

cssdashsubmit.php包括         

    session_start();
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit(); 
    }

    echo $_SESSION['flag'];

    if (isset($_SESSION['flag']) && $_SESSION["flag"] === 0) {
    $sql = "SELECT * FROM Ticket WHERE ticket_close_open = 'open'";
    $result = $link->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()){
            echo $row['ticket_id'];
            echo $row['ticket_equipment'];
        }
    }
    unset($_SESSION['flag']);
    }

    if (isset($_SESSION['flag']) && $_SESSION["flag"] === 1) {
    $sql = "SELECT * FROM Ticket WHERE DATE(ticket_open_datetime) = date('Ymd')";
    $result = $link->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()){
            echo $row['ticket_id'];
            echo $row['ticket_equipment'];
        }
    }
    unset($_SESSION['flag']);
    }
    ?>

现在,当我点击按钮时,它总是回显3,无论我点击哪个按钮。我尝试过更改会话变量名称,但它仍然存在。任何人都可以指出我错误的地方吗?

1 个答案:

答案 0 :(得分:1)

而不是会话 - 使用简单的url参数:

$('#openjob').click(function() {
    $.ajax({
    type: "GET",
    url: "cssdashsubmit.php?type=jobs",             
    dataType: "html",   //expect html to be returned                
    success: function(response){                    
        $("#responsecontainer").html(response); 
       }
    });    
});

$('#jobtoday').click(function() {
    $.ajax({
    type: "GET",
    url: "cssdashsubmit.php?type=requests",             
    dataType: "html",   //expect html to be returned                
    success: function(response){                    
        $("#responsecontainer").html(response); 
    }
    });  
});

在服务器端代码可以是:

session_start();
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit(); 
}

switch ($_GET['type']) {
    case "jobs":
        $sql = "SELECT * FROM Ticket WHERE ticket_close_open = 'open'";
        break;

    case "requests":
        $sql = "SELECT * FROM Ticket WHERE DATE(ticket_open_datetime) = date('Ymd')";
        break;
}
$result = $link->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()){
        echo $row['ticket_id'];
        echo $row['ticket_equipment'];
    }
}