...

View Full Version : AJAX with PHP function



icklechurch
04-11-2011, 11:00 AM
Hi,

I'm trying to call a PHP function from a static web page to get the progress of a file download, but it isn't working.

Basically I want the get_progress() PHP function to be called and give me the return value every 2 seconds on the web page, without having to refresh the webpage.

So here is the code at the top of the webpage:


<script type="text/javascript">
var guid = "<?=$guid?>";
var progress_int = "<?=$job_progress?>";

$(document).ready( function() {
setInterval( function() {
var value = parseInt($('#progress_id').text());
if(value < 100)
{
$.ajax({
type: "POST",
url: "/xml/order_status.php?guid="+ guid,
dataType: "xml",
success: function(xml) {
$(xml).find('order').each(function(){
var progress_int = parseInt($(this).attr('job_progress'));
});
}
});
$('#progress_id').html("" + progress_int + "");
}
else
{
$('#progress_paragraph').html('Your files have been successfully prepared.<br>Please see below for your download links.');
}
}, 2000);
});
</script>
<? flush(); ?>

This calls the following page, order_status.php:


<?
session_start();
require_once '../config/definitions.pso';
header('Content-type: text/xml');

$guid = urldecode($_REQUEST['guid']);
if(isset($guid)){
$xml_file = GET_JOB_ADDRESS . $guid;
$job_progress = get_job_progress($xml_file);

} else {

$job_progress = 0;

}
?>
<order job_progress="<?=$job_progress?>" />

Which should return the progress of the job as an interger that is returned and updated every 2 seconds here:


Order status: <?=$job_status?> (<span id="progress_id"><?=$job_progress?></span>&#37; complete)<br />

I'm new to this so please be patient.

Thanks,

Gemma

oesxyl
04-11-2011, 11:20 AM
- put a alert(guid); and alert(progress_int) before $(document).ready and see what you get
- replace short tags '<?' with '<?php'. i would also replace '<?=' with '<?php echo '

best regards

icklechurch
04-11-2011, 11:24 AM
Hi,

I've checked out what's going in on Firebug and the code is sound - it just appears to be caching the XML file so it always returns the same progress.

Any ideas?

oesxyl
04-11-2011, 11:39 AM
Hi,

I've checked out what's going in on Firebug and the code is sound - it just appears to be caching the XML file so it always returns the same progress.

Any ideas?
who cache xml? firefox, a proxy, your server?

in php you can use this:


header("Expires: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");


try to disable cache in firefox, i usualy do this when i write code.

best regards

devnull69
04-11-2011, 11:54 AM
Also there is a flaw in this piece of code:


$.ajax({
type: "POST",
url: "/xml/order_status.php?guid="+ guid,
dataType: "xml",
success: function(xml) {
$(xml).find('order').each(function(){
var progress_int = parseInt($(this).attr('job_progress'));
});
}
});
$('#progress_id').html("" + progress_int + "");

Due to the asynchronous nature of asynchronous requests the program flow will be
1. Start the request
2. Change the content of the element with id "progress_id" to the undefined variable progress_int
3. Some time later the request finishes and sets progress_int as a (local) variable

instead of what you expected
1. Start the request
2. Request finishes and sets progress_int
3. Change the content of the element with id "progress_id" to the content of progress_int



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum