how to count and display realtime database queries using ajax and PHP? - php

It gives great effect when you stream info in real time using ajax. for this example a SEO / webgrader tool called www.teqpad.com gives a nice interface and real time information on each queries into its database. They use PHP/ajax for the purpose.
My questions are:
how do they do it?
What are the methods or steps to take care of?
How php need to be written in to show the same.
I am not asking code but the method or proceedure to write the same.
Thanks in advance

Use the javascript setTimeout() function to invoke GET requests to the server.
Server returns data from your database.
HTML is updated.

You do an AJAX call every few seconds with javascript and refresh the HTML of the page with the response of the call.
You could use JQuery to do the AJAX calls:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
The call can be done to a PHP script. Depending the kind of information you want to get is the kind of operations you do on PHP.
Here is a simple example: http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp

Related

Can I wait for a js process within PHP before continuing?

I have a PHP script I use to register users but during that process I'm trying to call a Google GEOCoding javascript method to return the client lat/lng and record it to a DB before continuing with my PHP registration process.
Can this be done or is there a better way?
My current code looks something like this:
private function process() {
$this->validate();
$this->register();
// execute javascript here to record lat/lng coordinates to the DB.
$this->goHome();
}
You don't need javascript. The Google Geocoding API is a web service. In PHP, just do a file_get_contents() and issue the request using the proper url format as described here. You'll then need to run the result through PHP's json_decode().
You can't just run JavaScript in a PHP script. PHP is executed on the server, and JavaScript is executed in the client's browser. You should modify your registration form so that the JavaScript is called before the form is submitted, and you should pass the user's lat/lon as extra data for registration. Then, when your PHP registration runs, you'll have access to this data.
A better idea would be to find a PHP library to do geocoding.
See Johnathan M's answer.
Javascript is a clientsided script language
Php is serversided
This makes it impossible the way you imagion to do it.
Take a look into GeoIP
http://php.net/manual/en/book.geoip.php
You generally handle only one request in script and what you asking about would require at least two: in first user would ask for some response from your server and you'll be able to send your JS to him and in second request he'd submit results of running that JS.
You can save "state of waiting for response" in some way and process geo response when it comes.
I think the easiest way to solve your issue would be to use 2 AJAX calls and a function:
- the first calls a PHP script to do your validation/ registration
- when this completes run a clientside JS script to do the geocoding
- have another AJAX call to update the DB with the result of geocoding
You should be able to solve your problem this way. If you use a library like jQuery its easy to do the AJAX calls and you can even make use of deferred objects, which I think could help you chain your calls http://api.jquery.com/category/deferred-object/
Do the GeoIP lookup in PHP using a web API instead on the server side.
For example:
http://www.maxmind.com/en/web_services
They have a PHP example here (I wouldn't call the webservice that way, but it works):
http://dev.maxmind.com/geoip/web-services#PHP-11
private function process() {
$this->validate();
// Do a call to the webservice here with PHP, update user data with the response.
$this->register();
$this->goHome();
}
There are a couple of ways to go about it.
The best appoach would be to make the geocaching call within your PHP, not using javascript all. PHP has several ways of making HTTP requests from code, that will return results you can use in your script. I personally favor cURL: http://php.net/manual/en/book.curl.php. See also Jonathan M's earlier reply, same idea but using file_get_contents().
Another (far hackier) approach would be to make the javascript call to the Google Geocoding API synchronous (async=false in jquery.ajax()), and have it execute your request to your registration php page through a callback on the ajax call. (The "success" property on the ajax.) This means that your registration page will not be called until the geocoding lookup is completed. It's a valid question whether you want to make success/failure of your registration dependent upon completion of the ajax request, but if you do, this would work. Check here for info on synchronous ajax requests in jquery: http://api.jquery.com/jQuery.ajax/. This really isn't the best way to go about it though. The geocoding lookup really is part of the registration, and the last thing you want to do is make your php registration dependent on unreliable frontend behaviors.

How to call a PHP function when "onchange" event is fired?

I have three identical drop down lists that I'm populating using MySQL data.
If one of them changes index (onchange is fired), I want the other two to re-populate, so that they exclude the item that has been chosen with the other drop down list.
In ASP.NET, I would simply have called a function (passing through the chosen items' value) in the code behind to re-populate the other two.
I've been poking around for a while now and I just can't get it done with PHP. Does PHP allow function calling from this event? I've also tried using AJAX, but nothing.
You can't (directly) call server side functions from the client side.
You can make an HTTP request to a URL that runs a server side script that calls the function.
ASP.NET has (I believe, I've never used it) some stuff that will set up a URL and generate some JavaScript that will call that URL for you.
There is nothing in the PHP core to do that.
A very rough, quick and dirty outline (that makes no attempt to be RESTful) of how you might go about doing something like that would be:
callFunction.php:
<?php
include("myFunctions.php");
if ($_GET['call'] == "functionA") {
functionA();
}
?>
Then your JS would make an ajax request to callFunction.php?call=functionA.
OnChange() is a js function , in order to use it to call a php function you'll have to use AJAX . Using ajax you'll be able to get the php's code returned value , display it in the page , or doing whatever manipulation you'd like.
Good luck
ASP.NET would have been doing a Postback when the dropdown was changed, although you'd not have had to write it explicitly.
If you want to do a round trip to the server in PHP, you'll have to implememnt the same logic (e.g. post the form with JavaScript in the onchange event).
This is a good situation for using AJAX as well, but you haven't provided any details of what you tried or what went wrong so it's hard to help further.

javascript vs. php : pros and cons for code development

If a user refreshes a page I need to send the data using php as it accesses a mysql table.
If the user adds content, I don't want to run an AJAX call "first" as I can simply and immediately update the DOM, and then send a one-way ajax call to store it in the mysql table.
So on a referesh I have PHP creating my XHTML and sending it to the Browser.
On user input, I have the DOM update immediately followed by ajax call to put it in the mysql table.
Thing is I have to write code in JS and PHP for each user action that modifies the page.
Should I have the data sent to the Javascript for entry into the DOM and not do less with it in the PHP. What are the tradeoffs from taking user input and converting it to the UI with javascript vs. php?
Should I offload as much as possible to the client to reduce server load?
You have answered it yourself:
With php you need to send it through ajax and wait for response
With javascript you need to maintain 2 set of templates (server- and client-side one)
If you need to do something with the data serverside (validation, processing, etc), you can either use JavaScript with AJAX, or send it off on a page reload using POST or GET, depending on what you're sending. If you don't need to do anything with the data serverside, then using JavaScript to modify the DOM immediately is fine.
DOM operations aren't fast. Try it and you'll see that is better for your users to pass real HTML as it is or JSON-embedded with AJAX requests. Even big names like Twitter do it so.
If you still want to get away without PHP consider server-based JavaScript, e.g. Node.js.

jQuery AJAX get PHP script, display the content as it loads

I'm having to interact with the Facebook API for this project, which I find to be actually a bit slower than I expected. Because of this, I'm having to do something which I find rather unorthodox: I need to load the content Facebook provides back in my PHP script AS IT LOADS from Facebook. Traditionally I've loaded content into a div tag at the success of the script; however, I need to load the content as it appears. It would be absolutely unacceptable to have a client wait nearly a minute for Facebook to load an album and all respective comments before displaying anything. Hopefully I'm not being to vague; I'm not here to ask for code, but I've tried just about everything I can think of. Is this a simple concept I'm missing?... I feel as though this is easier than I'm making it.
I'm using jQuery AJAX as I find this easiest to work with. Any comments and/or help would be greatly appreciated.
The root of your problem is that jQuery's AJAX methods hook into the onreadystatechange event and readyState variable. readyState is only set to 4 when the file is completely transferred, and therefore your events will only fire after the download is complete.
Accessing the data as it is being sent is not consistent across different browser families. Doing it this way is going to be incredibly complex and time-consuming. I would recommend first doing this a little differently, perhaps by preloading the relevant facebook data on your own server predictively. This can be compiled to a static page, and that can in turn be served to your users very quickly.
To get the data to your users faster, you'll need to work outside the box as well. There's a jQuery plugin discussed here ( Does PHP flush work with jQuerys ajax? ) that makes jQuery ajax methods compatible with streamed output. Good luck.
The problem seems to stem from the fact that you're getting too much data at once. I suppose you are talking about receiving content from ajax as it is printed out immediately, but it is possible this content is built and sent at once and you won't have access to the data until the entire parse is complete. If this is untrue, look into COMET. If it is true, the solution is to put a limit on how much data you retrieve at once in an effort to reduce the parse time. For example, retrieve 5 photos in each request. Add those 5 photos to the DOM while you retrieve the next 5.
Instead of putting whatever code you want inside of the callback, just put it after the callback
for example
$("#div").load("facebook...", function() {
//do stuff
});
//put the stuff you want to load at the same time, here

update mysql detabase by using ajax

how we can update mysql detabase by using ajax...
any help or consept..
Ajax will have to call a remote script which will have to take care of the databse interaction. The code depends on the script language your database handling script will be written in.
You may have a look here for an example of ajax and mysql interaction.
You need to call a url from javascript, and do the job serverside.
We cannot update mysql database by using ajax!
Thus, divide your task into 2 parts:
make ordinal php script that updates a database by usual POST request.
debug it and make it to work
make AXAX code to make such a request.
debug it and make it to work
this way you can get your application working

Categories