0

Problem:

How do I include a token? My show_aht.php is being cached. I have to refresh manually my show_aht.php in order to get new data in my map.php when I get on the aht_button to make the AJAX call. Its very frustrating.

When aht_button is clicked it returns data, but if I refresh the page and/or I reclick the button it will still show me the old data or do nothing at all. I have to manually refresh my "show_aht.php" on my browser and then click on "aht_button" so I can display the new data being retrieve from "show_aht.php".

I did not want to post my PHP code because its a lot of stuff.. maybe someone can find the problem because I have no clue. Not sure if we can reload a PHP script by itself? I've put only the important stuff.

thanks in advance!

map.php JS:

<div id="aht">
    <button id="aht_button">AHT</button>    
</div>

<script type="text/javascript">
        $(document).ready(function() {
                $('#aht').click(function(){
                    $.ajax({
                    type:"GET",
                    url : "show_aht.php", //use a token here??
                    data:{ } ,
                    dataType: 'json',
                    success : function(data){
                        //get the MIN value from the array
                            var min = data.reduce(function(prev, curr) {
                                return isNaN(+curr['aht_value']) || prev < +curr['aht_value'] ? prev : +curr['aht_value'];
                            }, 1000000);

                            alert("min:" + min); 
                            //get the MAX value from the array
                            var max = data.reduce(function(prev, curr) {
                              return isNaN(+curr['aht_value']) || prev > +curr['aht_value'] ? prev : +curr['aht_value'];
                            }, -1000000); 

                            alert("max:" + max);
                            //function for calculation of background color depending on aht_value               
                            function conv(x){
                                return Math.floor((x - min) / (max - min) * 255);
                            }

                            //function for background color, if NA then show white background, either show from green to red
                            function colorMe(v){
                              return v == 'NA' ? "#FFF" : "rgb(" + conv(v) + "," + (255-conv(v)) + ",0)";
                            }

                        //going through all DIVs only once with this loop
                        for(var i = 0; i < data.length; i++) { // loop over results
                        var divForResult = $('#desk_' + data[i]['station']); // look for div for this object
                        if(divForResult.length) { // if a div was found
                            divForResult.html(data[i]['aht_value']).css("background-color", colorMe(data[i]['aht_value']));
                        }//end if
                        }//end for  
                    }//end success
                });//end ajax   
              });//end click
            });//end rdy
        </script>

show_aht.php:

include 'db_conn_retca2003.php';
include 'db_conn_retca2001.php';
header('Content-type: application/json');

    /****************************************************
    matching USER array and MEMO array 
    for matching username values
    /****************************************************/
    $result = array();
    foreach ($memo as $username => $memodata) {
    if (in_array($username, array_keys($user))) {
    // Match username against the keys of $user (the usernames) 
    $userdata = $user[$username];
    //if AHT is null give N/A as value
    if (is_null($memodata['aht_value'])) {
        $result[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => 'NA',
                                             'station'  => $userdata['station']
                                            );
    }//end inner if 
    //else give the actual value of AHT without the decimals
    else {
        $result[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => substr($memodata['aht_value'],0,-3),
                                             'station'   => $userdata['station']
                                        );
    }//end else
    }//end outer if
    }//end for
  echo json_encode($result);
?>
alda1234
  • 204
  • 1
  • 4
  • 15
  • Could send along a query string variable that is always unique like the date, time, minute and seconds. – iCobot Nov 25 '14 at 20:39

1 Answers1

3

There's multiple ways you can stop browser caching. One is to send headers that indicate no caching. From How to control web page caching, across all browsers?:

The correct minimum set of headers that works across all mentioned browsers:

Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0 

Using PHP:

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1. 
header('Pragma: no-cache'); // HTTP 1.0. header('Expires: 0'); // Proxies.

Using HTML:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> 
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

Note that this method depends on browsers respecting the no cache headers. This is not a guarantee.

You can also add a query string variable that contains the current time stamp when linking to those files you do not wish to be cached. Since you're going to a different URL every time, the browser will not cache.

Community
  • 1
  • 1
Kai
  • 3,803
  • 1
  • 16
  • 33
  • I tried using that before but it keeps telling me **Warning: Cannot modify header information - headers already sent by line 134** which is my echo json_encode ? thanks – alda1234 Nov 25 '14 at 20:26
  • You have to set headers before you send any other data to be rendered by the browser. Once data is sent to be rendered, the headers get sent. So, make sure you add those lines to modify the headers before line 134. – Kai Nov 25 '14 at 20:29
  • yes I did that also. However, for some reason its slowing the process of seeing the results to get displayed for about 2-3 seconds. Is there something I can do to make it "faster"? – alda1234 Nov 25 '14 at 20:34