1

I have the following code:

<?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 includes

    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']);
    }
    ?>

Now when I click on the buttons, it always echoes 3, irrespective of which button I click. I've tried changing the session variable name, but it still persists. Can anybody point where I am erroring?

15
  • Do you understand that line $_SESSION['flag'] = 1; does not execute when you click your button? Commented Jun 17, 2017 at 14:37
  • You cannot run PHP code inside a javascript code fragment. Remember javascript runs in the browser, the PHP interpreter only runs on the server. Commented Jun 17, 2017 at 14:37
  • oh I did not know that. Is there any way to achieve what i want? Commented Jun 17, 2017 at 14:38
  • @AkritiAnand Yes, pass 'flag' and 'x' through ajax, as part of data arrays. And set then session values in cssdashsubmit.php. Commented Jun 17, 2017 at 14:42
  • Thank you so much @aendeerei Commented Jun 17, 2017 at 14:43

1 Answer 1

1

Instead of session - use simple url parameter:

$('#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); 
    }
    });  
});

On server side code can be:

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'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.