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?

Dexter
  • 175
  • 1
  • 12
Akriti Anand
  • 166
  • 3
  • 17
  • Do you understand that line `$_SESSION['flag'] = 1;` does not execute when you click your button? – u_mulder Jun 17 '17 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. – RiggsFolly Jun 17 '17 at 14:37
  • oh I did not know that. Is there any way to achieve what i want? – Akriti Anand Jun 17 '17 at 14:38
  • @AkritiAnand Yes, pass 'flag' and 'x' through ajax, as part of `data` arrays. And set then session values in cssdashsubmit.php. –  Jun 17 '17 at 14:42
  • Thank you so much @aendeerei – Akriti Anand Jun 17 '17 at 14:43
  • @AkritiAnand welcome. –  Jun 17 '17 at 14:44
  • @AkritiAnand Put your buttons in a div. Put your `script` in the `head`. Use `$(document).ready(function () {...}`. Use `POST`, not `GET`. –  Jun 17 '17 at 14:48
  • @AkritiAnand Use PDO instead of mysqli. Use ALWAYS prepared statements. Use ALWAYS exception handling. Sorry for too much todos :-) –  Jun 17 '17 at 14:50
  • @aendeerei since i am just trying to implement different cases of ajax request to the same php, i had written the front end shabbily. As for POST and GET, why do you suggest I use post? – Akriti Anand Jun 17 '17 at 14:52
  • @AkritiAnand What's shabilly? :-) –  Jun 17 '17 at 14:54
  • @aendeerei :P i have not written a good html code. Although thanks for all the suggestions. I have just started learning PHP from the net and do not know many good practices. – Akriti Anand Jun 17 '17 at 14:57
  • @AkritiAnand Oh, I see :-) Now, POST vs GET: when you use a form to get user input you use POST, right? User can change data. He can change it always when you use Get, too. But in your case you use ajax. So, if you use POST to fetch data, then nothing can be changed by the user. –  Jun 17 '17 at 15:00
  • Oh i see. Thanks @aendeerei – Akriti Anand Jun 17 '17 at 15:04
  • @AkritiAnand I give you a link to an answer of mine where all these "good practices" are included. It's very understandable and, of course, commented. Just follow the code in "deli_list.php" and you'll have a good app. –  Jun 17 '17 at 15:04
  • @AkritiAnand Here it is: **[HERE](https://stackoverflow.com/questions/44594328/mysql-dynamic-link-to-fetch-the-right-row/44594936#44594936)**. The page `deli_list.php` is presented as the last and is the main page. It's about the accepted answer. Good luck! –  Jun 17 '17 at 15:06

1 Answers1

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'];
    }
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64