Jump to content

xmlhttp won't call my php file


cliquot22
 Share

Recommended Posts

I have a JS file that collects data from my GPS. I want to write it to a file so I am using xmlhttp to call a PHP file that should write the data. However, the writing only happens about 10% of the time and I can't figure out what the pattern is. Can anyone tell me why the PHP file is not being called (or if it is, how can I tell and why isn't it writing to the file)? Here's the JS code: onFinishReadFromDevice: function(json) { try { //.................. deleted lines .................... this.setStatus("Position data captured"); // save data to file xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", "recordPoint.php?lat=" + this.extractedLat.value, false); xmlhttp.send(); alert(xmlhttp.responseText); this.setStatus("Ready"); } catch (e) { this.handleException(e); } },which calls this php file: <?php$lat = $_GET["lat"];$file = fopen("cross state race track.txt", "a") or exit("Unable to open file!");$output = $lat . "\n";fwrite($file, $output);fclose($file);?> I can create and write a header to the file so I have permissions set correctly I believe. Also, "this.extractedLat.value" is not empty or null, there is a value. I am using the synchronous call to xmlhttp becuase I want to wait until the writing is done before setting the status to "ready". However, this doesn't work because the status is set to "ready" but there is no writing of the file. By the way, that is the entire PHP file, no header info and no html. Is that valid? Mark

Link to comment
Share on other sites

The code is valid, but there's not really any error checking. It may be good to do an asynchronous call, and handle the update in a callback function which could also report errors. Does the handleException function do any error logging where you can look that stuff up after the fact? For error checking with PHP, it would be good to add code like this to the top of the script so that all of the error messages get sent to a file which you can check. This will write errors to a file called error.log in the same directory as the script:

ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('display_errors', 0);error_reporting(E_ALL);

Link to comment
Share on other sites

No errors were recorded and I did verify that the error checking code was working. However, I discovered that the xmlhttp.open line only calls the PHP file if the URL is different. When I call the same URL over and over (URL="recordPoint.php?lat=45") the PHP file only writes data the first time. Does this have something to do with my cache setup? Is there a way to force the recordPoint.php file to reload? Mark

Link to comment
Share on other sites

You could try appending the current timestamp as another GET parameter. This will make the URL unique each time and so the caching should be bypassed. Try something like:

xmlhttp.open("GET", "recordPoint.php?lat=" + this.extractedLat.value + "&time=" + new Date().getTime(), false);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...