Server Side Reload Initiation on Ajax Call - php

I need to initiate a reload from the server side(PHP) durring and AJAX call. How do I do this. I honestly don't think it is supported.
My current method is to use the PHP header() function and capture it as responseText from the AJAX call and then do an innerHTML add.
However it is not a proper reload in my book as it doen not intiate the onload function. It also seems to to be contrived and not a "clean" solution.

Send a command/flag back to your Ajax handler to make it call the following JavaScript method in the page:
window.location.reload();

There is a pattern that uses AJAX to create a PUSH-alike application. I don't know the real name of the paradigm, nor a good implementation, but this is basically how it works:
Client makes AJAX request to server.
Server doesn't close the connection.
When server wants to push. server says:
a. Process push message X
b. Initiate a new connection. (i.e. goto 1.)
< Server closes connection >
So there is always a "hanging" connection, and as soon as the server wants to respond, the client immediately creates a new one that hangs again.

The only way to reload the entire page is to change the window.location. You can't do that from the server. Every action must be initiated from the client - that's fundamental of restful protocols like HTTP.
However, your server could send back the entire document, and you could then 'pick' the portion to replace. jQuery allows this with the load function.
For instance this would reload the BODY:
$('body').load('ajax/page.html body');

Related

I need to push data to browser of all clients which are currently browsing our web page

We are making a app with php in which we want to push data to client browser same in the way in which facebook notificaiton sent by facebook. i know about commet and also have used ajax in past but ajax is not efficeint while commet programing is out of my mind.
Sample can seen at ESPNCRICINFO.COM live scorecard which is automatically pushed
by server to brwoser and than append to document without refreshing. Same app we
have to made.
Simply i need to build some code that send data to all the browser which have our webpage opened. No restrictions. Just need to send to all. So there is no need to check to whom data will be push.
I really need it urgently.
can you use JavaScript setTimeout(function name, seconds) ?
In JavaScript function you can generate .post request for some PHP script which can load messages, when the message will load, you can show it via you JavaScript. the timer will call this function for all people.

how to detect user is disconnected via PHP?

Is there a way to detect whether a user is disconnected from internet or not? The way that stackoverflow does when you want to post a question. I couldn't think of any approach to do that. Could someone shed some light on the subject?
You can send AJAX-request to PHP script when windows is closed:
window.onbeforeunload = function(){
// Request goes here
}
Alternativly you can use websocket-technology (you can use phpDaemon) to connect with server permanently so you will know when user is disconnected from internet or your site or pereodicly (use setInterval function) ping your server.
I guess Stack Overflow uses AJAX, which is a JavaScript driven program executed on the client side inside your browser. This ajax setup is responsible for notifying the user when, for example, a new answer is posted, and giving them the opportunity to load said new answer without reloading the whole page.
And this construct has a way to detect errors in the communication with the server which it is interpreted as the user being disconnected, resulting in a warning.
However, this requires that the user is still having the browser open. There are also various other functions in JavaScript and AJAX to execute something when the user is closing the page, but neither of them are considered to always work. There are no silver bullets after all.
From the server's side, one can monitor the constant ping-pong of the user client's AJAX and execute something when this ping is fading away. Like: the user has been pinging us in every 5 second in the past two minutes, but now this ping is missing.
The main problem with this lies inside the principles of PHP and that every pages basically lives on its own. When the request is get, the page is loaded and created, but at the end of request, the current page instance is denied from existance, just like how every variable is lost which is not saved elsewhere (cookie, session, database).

What will happen when location.href is changed before ajax request completes?

User clicks some link which execute some ajax request - lets say this request takes 20 secs.
Before the request is complete user clicks oder link which redirects (no ajax) whole page to another page.
What will happen with ajax request? It will be always completed on server side but the response wont come anywhere? Or maybe ajax request on server side will be immidiately "killed?
I ask because I have some script which takes some time to run but user doesn't have to now the result - it's just fire and forget - maybe there is even some option in ajax to force it not to send any response?
The browser should kill the AJAX request, closing the connection to the server; however, this does not mean that your processing on the server is necessarily killed too: ignore_user_abort()
The server will complete the request, unaware that the client has "moved on." The server will return the response to the client like it normally does. The client will simply ignore the response.
So expect everything server-side to happen as normal, so the "fire and forget" method will work (since the client has moved on and has "forgotten"). But if you want to do anything client-side in response (which would negate the "forget" part) then there's no way for the new page to intercept the response. The browser will ignore it.

Using XHR progress events to monitor a MySQL query

I have a php script which is called by a javascript XHR function. In this script it accesses the Facebook API using a token (which is parsed from a Facebook redirect after accepting to connect to facebook).
When I add a progress event to the XHR object, it never runs, and when I log the readystates it gets to, only 2 and 4 are called. I don't think its necessarily a problem with my code, my only guess is that in my PHP script it gets the file contents of 'https://graph.facebook.com/me/albums?limit=0&access_token=' + $token (which json decodes its contents to use as an object which is looped through, adding each of the users' album(s) to a database) doesn't like the fact that I am visiting another url inside this XHR request.
Can anyone give me some guidance so I can make my progress bar 'load'?
Thanks!
The best way to do this is probably to just use good old readyState

Automatically Log-Out Users When They Close Their Browser

I'm using PHP as server side language and Flex as Front end. In my application there are different levels of users. According to operator's online status QC assigns some job online. I just want to update user's online/offline status when he closes the browser automatically. Flex calling PHP service but its not getting any response. DB(MySQL) is not getting updated. Any Help is appreciated.
As far as I know this is not possible. Your best bet is to save the datetime in the database for the user when the they log on and then run an AJAX script to update that user's time each minute in the database. Then you can check to see if the user has a datetime older than a minute to see if the user is logged in. You can adjust the "resolution" of this approach... checking every 5 minutes for example.
If you are wanting a service call made before the browser closes, I think you will need to have Javascript send a message to your flex/flash client, then have flex make the service call to let your server know the client/browser is closing. Once the service call (request/post) succeeds, flash client responds to Javascript to continue browser close. Caveat here is that I don't think you can consistently have javascript block from browser to browser. The browser may close before flash can get the service call initiated. Good luck.
BTW, you can register a call back listener using ExternalInterface and have javascript signal Flex using the javascript function referenced in the callback listener.
Thank you all. I was confused about AJAX but finally its done. I'm calling a function while closing browser. <body onunload='Logout();'>. Here's the function
var xmlhttp = new XMLHttpRequest();
url = "http://*****.com/public/LogOutThruJS.php?UserID="+UserID+"&LogHistoryId="+LogHistoryId;
function Logout()
{
xmlhttp.open("GET", url,false);
xmlhttp.send(null);
xmlhttp=null;
delete xmlhttp;
}
I hope its understable :).

Categories