Showing posts with label PHP Pull Dynamic Data. Show all posts
Showing posts with label PHP Pull Dynamic Data. Show all posts

Thursday, 30 August 2018

My ajax call does not pull dynamic data from PHP / Mysql


I am working on a project where I am going to have divisions from a league listed as buttons on a page. And when you click on a button a different team list shows for each division. 
All divisions and teams are stored in a mysql database and are linked together by the "div_id". The plan was have the buttons use javascript or Jquery to send the 'div_id" to a function; which would then use ajax to access an external php file and then look up all the teams for that division using the div_id and print them on the page. I have been piecing this all together and getting the various pieces to work. But when I put it all together; it seems like the ajax part - does not pull in fresh data from the database if the data is changed. In fact, if I change the PHP file to echo some more data or something, it keeps using the original unaltered file. So, if the data is changed that is not updated, and if the file is changed that is not updated. I did find if I actually copied the file with a new name and then had my ajax call use that file instead; it would run it with new code and the new data at that time. But then everything is now locked in at that point and cannot get any changes.

So - I do not know much about ajax and trying to do this. I am not sure if this is totally normal for what I am using and for a dynamic changing team list, it cannot be done this way with ajax calling a PHP file.
OR - maybe there is something wrong with the ajax code and file I have which is making it behave this way? I will paste in the code of my ajax code and also the php file…
here is the ajax call:
var answer = DivId;
$.ajax({
type: 'GET',
url:  'path_to_file/gscript2.php',
data: 'answer=' + answer,
success: function(response) {
    $('#ajax_content').html(response);
}
});

and here is the script.php file that it calls (removed db credentials):
<?php
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
    && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
    // AJAX request
    $answer = $_GET['answer'];
    $div_id=$answer;

    echo "div id is: " . $div_id . "<br/>";

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database!       Please try again later.');
mysql_select_db($dbname);

$result_g1 = mysql_query("SELECT * FROM teams WHERE div_id=$div_id");

while($row = mysql_fetch_array($result_g1, MYSQL_BOTH))
{
$team_id=$row[team_id];
$team_name=$row[team_name];
echo $team_id . " " . $team_name . "<br/>";
}

}
?>

So - to sum up - is there something wrong with this making it do this? Or is what it is doing totally normal and I have to find a different way?
Thanks so much...

Most likely your browser is caching.
Try adding cache: false as such:
$.ajax({
    cache: false,
    type: 'GET',
    ...

The jQuery documentation explains that by doing so, it simply adds a GET parameter to make every request unique in URL.
It works by appending "_={timestamp}" to the GET parameters.