What would be the best approach to give the user feedback, that a PHP script is running while they wait?
I've got a PHP script that runs via an AJAX call when a user clicks a button.
The PHP script deals with large heavy images and takes some time before it is completed.
Because it is activated via an AJAX call, the user get's no visual feedback that the PHP script is running, because the user does not leave the page.
How do I go about giving some kind of visual feedback that something is happening via PHP in the background?
A simple .gif that is visible during the loading time of the script would suffice.
The easiest way to do it is to show the image as soon as the click takes place. You could do this by having an image already in place styled with display: none and then use jQuery's show() function to show it.
When you get your success message within your AJAX call, hide the image again.
If you have incremental activity that you want to show textually you can also direct the action at an iframe and have php echo and flush.
echo "Beginning Step 1... <br/>";
flush();
// do step 1
echo "Step 1 Complete!<br/>";
echo "Beginning Step 2...<br/>";
flush();
// etc..
Related
This is a complex question so I'll try and be precise.
I've experimented with AJAX but this form requires many changes to do it. I'd like to know if there are other options.
I have a form which takes a very long time to complete. Let's say the form exists on page A. The form submits and calls page B.
Page B looks like this, and gives the incorrect result (the page appears blank while loading):
<html>header</html>
<?php
longformresult()
?>
If I setup the page like this I get the result I am looking for (the page waits until the long function is done to load the page, allowing me to display a loading screen on page A).
<?php
longformresult()
?>
<html>header</html>
however this is where things get really tricky, the longformresult() can fail and break everything below it.
Is there some way I can tell php to wait until the page is fully loaded before sending the page, allowing me to have a loading screen on page A? Or prevent a die() from killing my page? I essentially need to delay the appearance of Page B, and actually just a sleep() is probably the best approach I've had so far:
<?php
sleep(4)
?>
<html>header</html>
<?php
longformresult()
?>
Edit: Conclusion (Not the fix I was looking for though)
I used ajax and removed 'Page B' entirely.
PHP waits by default to complete the function call before proceeding (see this previous answer), so PHP necessarily waits for the page to be fully loaded before returning.
AJAX would not require "many changes" if this is truly the behavior of the program. You would simply have a new file like C.php which returns the output of longformresult(), which you can later inject into your page.
I need to run a php script (site crawler) on submit button click without a page reload. After the background script run, A confirmation / alert popup should appear that the site crawler is ended. Any Ideas Please?
Basically the idea goes like this:
you send and XHR call to server and execute script
script checks if there is already a background process
if there is one, return the status code
if on background script running: script forks itself , returns an "OK" response and start to do the work
with a different XHR call you repeatedly check if script has finished
when the server-side script has finished running, you gather the results and diplay in the page
To simplify the interaction with server, you might want to use some of the XHR libraries. I would avoid suggest to avoid slapping on jquery unless you are actually already using it for something else.
As for "how to know when background script has finished" : i think the best way would be for that script to create a file, which contains
when running: -1
when finished: 0
when error: positive number
Jquery + AJAX could make your job easier. Read this tutorial on how to achieve the result:
AJAX+ jQuery Tutorial
Here is what I've got right now...
I have a web page that when accessed, it connects to surveygizmo.com via their open API, retreives a whole lot of data, and then returns that data to me for processing on my end. This process takes roughly 10-12 seconds and while it is executing, the page just sits in the "loading" state and I'm shown a blank white page.
Here is what I want to happen...
When that same web page is accessed, I'd like it to kick off the script execution and begin retrieving the data from surveygizmo.com via their API in the same way I'm doing it now, but while that process is running, I would like there to be a "result are loading" page/message that is shown instead of the blank white page and then once the script execution finishes and all the results have been returned and processed, the "results are loading" page/message disappears.
I'm pretty familiar with php and javascript, but haven't been able to figure this one out yet. Any direction or advice on the matter would be greatly appreciated.
Thanks!
You'll want to take a look at AJAX. The solution is to send the browser a "loading" page, which contains javascript that shows the loading message, and then connects to the actual data generating server-side script asynchronously. When you have received and processed the data, you can insert it into the DOM and remove the loading message. Googling for XMLHttpRequest will get you the details.
Create a loading page that displays your desired loading message. Then using ajax (I HIGHLY recommend jQuery) load a separate php file that loads the outside data. Once the data returns to your loading page, it's a simple task of hiding the loading message and replacing it with the output of the ajax.
<?php
echo "Working...<br/>\r\n"; flush(); //output to the browser before the rest of the execution
connect_surveygizmo(); //this is going to take while.
echo "Finished!";
?>
I'm trying to find a solution to this problem: I have a page on a php application that can't be opened more than once to avoid session overwrite.
I'm spending a lot of time without finding any solution!
The last one I tried is to use the jquery "load" and "unload" events on the window. It works fine if I open the same page in a new window or a new tab but it also blocks the page if I refresh it!
I really don't know how to go on! I thought this was a simple example of semaphore usage...but I can't code it in the right way.
Any suggestions? (both php and js solutions are welcome)
You could try with a cookie:
The first page load, the cookie is set(by the server for the session)
The next time the same page is loaded, it reads the cookie
If the cookie is there, do not overwrite session variables
I don't think the browser will be able to make a difference between a refresh and a load in another tab/window.
Can't be opened more than once by the same user, I presume.
How did you use jquery's load and unload events? Brainstorming here, how about using load to set in the user's session a semaphore, which is removed by unload. Ofcourse I'm not sure if that event fires even when the browser crashes or similar unforseen technical issue. In that case you might have a stuck one that you'd need a specific timeout for, or to wait for the session to time out.
To fix that perhaps after the page loads have a timer that executes every 10 seconds, basically updating the semaphore sort of like "The page is still open", along with current timestamp. If on load the semaphore exists but the timestamp is older than 10 seconds, then the user is refreshing.
The problem is that you do not have access to the end user's pages that are currently open in a browser. So the only solution is for you to keep tracking the pages the user opened/closed/switched to another page/etc. Even that isn't perfect as the user can open another browser process. So there is no perfect solution to this.
sorry guys...you're right!
A bit of code will help all of us :)
suppose my script is script.php
in the html of script.php i put
$(window).bind('load',
function(){
$.post(PATHTOLOCK.php);
});
$(window).bind('unload',
function(){
$.post(PATHTOUNLOCK.php);
});
In the file PATHTOLOCK.php i do this:
$_SESSION['flag']=true;
And in the file PATHTOUNLOCK.php i do this:
$_SESSION['flag']=false;
At the beginning of script.php i put
if($_SESSION['flag']==true){
echo "error";exit;
}
If I open script.php in two windows/tabs everything works fine. If I refresh the page it doesn't work because I suppose the events sequence is the following:
click on refresh
unload event is not invoked because the page is not completely left
the script.php is reloading
the check at the beginning of script.php fails because flag=true
script.php goes in error
the unload event is invoked because the page is left and flag=false
click on refresh
now it works!
and so on...every two refresh it works!
I have a run.php that has a button on it called "Run". Clicking it does a jQuery $.get() AJAX call back to a response.php on the server, and sends the result to a Javascript callback function getResponse(sData) inside run.php's HTML for display back on the browser. I want to have things such that as response.php runs through a list of tasks, it echoes a response back with the echo command and for getResponse to update a DIV with that status as it moves along. So, let's say I have 5 steps inside response.php, and therefore 5 echo statements back to getResponse().
I tried to get this to work, but what happens is that jQuery waits and then sends one single response all at once, rather than sending as it goes along with the 5 responses.
What's the technique?
The reason I ask is that I have a script that does something to a bunch of files. The first thing it does is a file count, so it updates my progress bar. Then, as it runs through files, it needs to increment my progress bar like every 1000 files.
I think there's no way to make that ajax call to have multiple response in just one call... but what I could suggest is you make a session on php... and in every steps on your tasks function, update that session... then make another ajax call that checks that session if any updates happened... if there is update then do what you have to do....
As you can't really get progress with xmlhttprequest, I suggest you can look into other ways of doing AJAX calls. One of them is through iframe. You can create hidden iframe, set it's sources to request.php and then periodically just check it's content. It should be possible since it's all it the same domain and restrictions does not apply.
iframe might work because it's not that different from normal browser window, meaning that it periodically applies data it gets into DOM even if request hasn't been finished yet. There's potentially might be problems with how different browsers do that, i.e. IE shows new content only if it got more than 4K or something. But it is possible to overcome that, I'm sure.
So, create new hidden iframe, add src attibute to your php script, make that script periodically write something to the client and on the client check what have been written and convert it to shiny GUI stuff.