Loading bar while script runs - php

I have a php script which takes about 10 seconds to run because it is pulling in data and storing it in the db, I want to display a loading bar whilst this script is running and then once its done load the page the user is on...has anybody any ideas how I can do this? Thanks

At first you need to call the script using an Ajax request. All jQuery ajax methods offer function callbacks that are called when the call is completed. You can show the progress bar when you make the request and hide it, when it is finished:
$("#MyProgressBar").show();
$("#placeholder").load(myurl, function() {
$("#MyProgressBar").hide();
});
You could also use a plug-in like BlockUI.

Related

Calling a php function on page load

Hi I have the following php code that I would like to be able to have it fire on page load. Any idea how this can be done.
action('clear_session')?>
thanks Maria
PHP runs on the server, but the PagLoad event occurs on the client. You can't directly call a PHP function from the PageLoad event. You might be able to do what you want while PHP is generating the page, or you could perhaps use an AJAX call to achieve the desired result.

PHP MySQL Requery vs Refresh

I have a webpage that run a query on a db and reports the results. Currently the page auto refreshes itself every 10 seconds in order to display (almost) real-time data.
This is probably a very inefficient way of accomplishing this but as of right now I'm not really sure what alternatives there are.
What options do I have to present real-time data on a php webpage?
you can use node.js and socket.io. This is great example to get you started:
http://book.mixu.net/ch13.html
or you can use ejabberd and strophe js library.
I used both of that solution for real time stuff and I found node.js and socket.io much easier to implement.
Depending on what your data is for and how you are using it, ive recently started putting data display functions into seperate pages and then using jquery to load those pages into dives on my main page at page load and then reloading the divs on a timer and on a reload function if the refresh is needed sooner, that way your whole page does not refresh making it flash and go blank but instead the data just gets nicely updated.
var paused = false,
auto_refresh = setInterval(
function()
{
if (paused) return false;
$('#mot').fadeOut('slow').load('motview.php?view1=$dayd&viewdate=1').fadeIn('slow');
$('#work').fadeOut('slow').load('workview.php?view1=$dayd&viewdate=1').fadeIn('slow');
$('#motday').fadeOut('slow').load('motdaylist.php? view1=$dayd&viewdate=1').fadeIn('slow');
$('#notelist').fadeOut('slow').load('notelist.php?dayd=$dayd').fadeIn('slow');
}, 60000);
$(document).ready(function(){
$('#mot').load('motview.php?view1=$dayd&viewdate=1');
$('#work').load('workview.php?view1=$dayd&viewdate=1');
$('#motday').load('motdaylist.php?view1=$dayd&viewdate=1');
$('#notelist').load('notelist.php?dayd=$dayd');
});
function reloaddivs()
{
$('#mot').load('motview.php?view1=$dayd&viewdate=1');
$('#work').load('workview.php?view1=$dayd&viewdate=1');
$('#motday').load('motdaylist.php?view1=$dayd&viewdate=1');
$('#notelist').load('notelist.php?dayd=$dayd');
};
the above im using to display 4 lots of data on one page that all updates nicely on a timer or after ive interacted with a page by calling reloaddivs
Inspect your page carefully to identify exactly what is the data which is to be updated every 10 seconds - chances are it will not be the whole page (menus, navigation, headers, footers).
Cache all that data say, every 10 minutes, or every hour - especially if data driving any of those elements are being dragged from databases.
Then include the panel containing the dynamic data.
Better yet might be to load the dynamic data using Ajax to refresh every n seconds.

Run PHP script in background on submit button click without page reload

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

use jquery ajax to refresh PHP data on same page

I have a PHP page with a mysql connection, a select query and then i'm building a table using PHP. If i wanted to use jQuery AJAX to refresh the data on a setInterval, on the same page, how would i go about doing that? (by the way i can do it to another PHP page but i've never done it if the PHP stuff is on the same page)
If you want to keep making ajax calls i suggest you do long polling, which basically means you have a script that is requesting content via ajax every given time, and it will verify every time if the content has been modified, if not it will wait again and make another call.
I used jQuery Periodical updater for a chat box and it worked perfectly.
If you wnat to learn more about how jQuery and AJAX work, check out this Nettus article
You set the param in request, for instance file.php?ajax=1
The, depending on its value, you render full html or just the necessary elements for ajax
in php:
if($_GET['ajax']) renderAjax();
else renderFullHTML();
in js:
$.get('file.php?ajax=1', function(data) {
$('.result').html(data);
});

How To Make A jQuery Async Multi-Response From One PHP Script Run

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.

Categories