Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    96
    Thanks
    11
    Thanked 0 Times in 0 Posts

    AJAX with PHP function

    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:

    Code:
    <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:

    Code:
    <?
    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:

    Code:
    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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    - 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

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    96
    Thanks
    11
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by icklechurch View Post
    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:
    PHP Code:
    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

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Also there is a flaw in this piece of code:
    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

  • Users who have thanked devnull69 for this post:

    oesxyl (04-11-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •