开发者

PHP AJAX Scripting to Get a PHP Variable

开发者 https://www.devze.com 2023-02-22 03:32 出处:网络
I am new to using ajax but have been using PHP for awhile. I am curious on how to combine the two. I hav开发者_如何学Pythone a PHP script on my website that takes awhile to loud because it is perform

I am new to using ajax but have been using PHP for awhile. I am curious on how to combine the two.

I hav开发者_如何学Pythone a PHP script on my website that takes awhile to loud because it is performing multiple iterations and many functions.

I want to somehow develop like an ajax script that tells the location of the script. There is one variable throughout the script that is like a progress meter. Somehow I want the text to change every time that variable changes.

Is this possible if so how?

Help is greatly appreciated.


You can save the current progress meter state in a file, ram cache or database and write another script that just returns this state. Then you implement AJAX that retrieves this information and then start a new ajax request and so on until the progress is finished. I'd suggest something like "long-lived HTTP connection" using AJAX. Pass the state in your ajax request and if the progress didn't change in PHP, just sleep until it does.

Like this (state in %):

AJAX request: progress.php?state=0
AJAX response: state: 10
AJAX request: progress.php?state=10
-- on server side it is still 10%, so sleep the php progress until it changes and then reply
AJAX response: state: 11
....

And since there is a timeout for ajax request just start a new request once a request timed out.

I hope this is what you meant.


jQuery.ajax() doing what you want:

Posting data to a PHP script then getting its result:

$.ajax({
  url: "progress.php",
  success: function(response){
    $("#results").html(response);
  }
});

jQuery UI even has a Progressbar widget, it'd be a wise decision to combine both.


Perhaps within your server-side script that takes a while to load, create a "reporting variable" that increments at a measured series of junctures. Create an independent server-side AJAX callback method to peek at the reporting variable and just echo that number. When it is received on the client side it updates that portion of the DOM and viola`, no mess, no fuss.

0

精彩评论

暂无评论...
验证码 换一张
取 消