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
Related
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.
So the question is a little complicated, let me explain. My page code is running like this:
User enters query in the search field and clicks submit.
1.1 jQuery loads a new body to display progress data.
1.2 jQuery calls process.php via AJAX and supplies query as the argument.
1.3 jQuery starts setInterval periodic update to grab progress data, stored inside $_SESSION['prog'], and displays it.
When process.php finishes, jQuery stops periodic update, displays final information and calls AJAX to clear the $_SESSION['prog'] variable.
At the moment progress data is stored inside one variable, which is fine as far as different users are concerned (because of the different sessions), but if the same user were to make multiple requests at the same time, the $_SESSION['prog'] variable would be cross-overwritten.
So far I have thought of two possiblities to distinguish data for each request from the same user (same session)
Have jQuery generate some random string and send it together with query (and hope to avoid colission, although that would be unlikely)
Make 2 AJAX calls, first one requesting new_request_id, the second one sending query and new_request_id as parameters.
Have AJAX return something from PHP before is finishes(completes).
I need to connect each browser window (each request) with each running process, so I cannot send back new request ID after the request has been submitted, because I wont know which data to pick up with jQuery in the browser window. Btw, I will change $_SESSION['prog'] to $_SESSION[request_id] -> request_id is what I'm looking for.
It (request_id) could be last_insert_id(), because im creating new DB entries for each valid query, but I don't know how to get it back to each different user window.
I need advice here. Only just begun to code in PHP, AJAX and jQuery, don't really know much about sessions. How should I solve this problem?
Sorry for the lack of code, I will paste is at request.
You could add a unique ID to each request in addition to the session ID. eg. uniqid() in javascript/jquery?
You need to differentiate them somehow. For example use a unique ID autonumber field. MySQL has last_insert_id() which is very useful and handles concurrent requests correctly.
Avoid using Session variables in Ajax requests. Send them with GET (or POST) instead. Even if calling Session_start(); in the Ajax request and getting $_SESSION['prog'] from there, results can be unexpected.
Here is the scenario:
I have a page that is logging data to MYSQL. I have another page that reads that data and allows it to be viewed. When a new piece of data is logged I would like to have the first script check and see if the viewing page is open in the browser, and if so append the newest data to the end of the view. Also - could anyone point to some info giving an overview of how PHP and the browser interact? I think I have the concept of the DOM down for javascript...but as far as PHP it just appears that once the page is sent, that's it...
You're correct in that once the PHP is sent, that's it.
There is no way to send data to a PHP page once the page is loaded. There is another slightly nastier method, but the easiest way of doing this is going to be polling the page via Ajax.
So, have a script that every 20 seconds, sends a message to another PHP script that contains the timestamp of the last MySQL log you received, then get the script to return all the data that has been set by that time.
I'm unsure how new you are to JavaScript, but the easiest way of doing that is probably using JQuery's $.ajax and encoding the new MySQL records as JSON.
No this isn't possible as you describe. The viewing page will have to poll the server for changes, either by periodically reloading itself, or by javascript / AJAX.
You are right that once the page is sent by PHP it can have no further influence. In fact the PHP execution thread on the server is killed as soon as output is complete, so the thing that generated the page no longer even exists.
To expand on Dolondro's suggestion, rather than periodically polling the server for updates, you could use Server-Sent-Events (newly supported in modern browsers).
With these, you basically just send 1 ajax request to the server, and the connection is held open. Then, the server can send updates whenever it wants. When the browser receives an event, it can add the data to the screen. Even still, the connection is held open, and the server can send additional events/updates as they occur.
W3C page:
http://dev.w3.org/html5/eventsource/
Wikipedia:
http://en.wikipedia.org/wiki/Server-sent_events
More Info:
https://www.google.com/search?ix=hcb&sourceid=chrome&ie=UTF-8&q=server+sent+events
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 :).
when i update score from my admin i want to update score in client autometically without refresh can any one help with script and technices
i see such auto refresh https://stackoverflow.com/
ANSWERS , VIEWS autometically updating
The only way I know to do this is to actively poll your server from the client.
You'd need to create some polling script that called your script service page with a request for the data. Then, when the data is saved in the admin page, the service will return the latest scores when next asked.
Typically, you'd use setInterval and clearInterval. So on page load, you'd assign your polling function call to the setInterval method and give it a timeout of something sensible (10 seconds plus depending on how often you expect to update your scores and how big your traffic is).
You'd need to be using clearInterval whenever you want to stop the polling. As per the other answer, a JS framework will help you with making the Ajax requests whether in Xml or JSON format. Given your tags imply 'faster' then I'd recommend JQuery and JSON.
Additionally, whatever framework you use, consider using Googles CDN for fast deployment of that framework.
I'm unaware of any server push with Ajax so would be interested to see if that methodology is available (though I doubt it).
EDITS: added more info on Google cdn and frameworks.
HTH,
S
You'd probably want to look into a js framework.
The steps you'd go about doing this are similar to:
Add an onclick listener to your link /button
When that gets trigger, stop the event (this way you stop the browser from refreshing / redirecting the page)
Get the user submitted data and send it to the server via an json ajax request
Process data server side and return a response
Process the response and update the interface accordingly
Here is a bit more documentation on how you could do this with mootools:
* http://mootools.net/docs/Request/Request.JSON if you want to go with the JSON path
* http://mootools.net/docs/Request/Request.HTML if you just want an AJAX updater
While I haven't used it, it sounds like this is what "comet" is intended to solve. It's a way of streaming content from a server to a client - basically a "push" approach. It might be what you're looking for.
http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications
There is no need to refresh the page if you use AJAx.
With AJAX you can call the server and place the result in a portion of your web page without refreshing the page.
There is an article here regarding using AJAX via jQuery.
The jQuery documentation on AJAX is good too.
An example which would request data from the server and append the result to a section of your page is shown below:
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
$("#results").append(msg);
}
});