How to recive mysql data using javascript ajax?

03-21-07, 01:13 PM
|
|
Wannabe Coder
|
|
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to recive mysql data using javascript ajax?
Hi all. could any one show me how i can recive and display mysql data using javascript ajax and php?In my case i want to recive multiple data.
I also want to know how to send data to server using javascript ajax .Thanks
|

03-21-07, 01:19 PM
|
 |
Community Liaison
|
|
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
|
|
search google for "AJAX and PHP", that's how i did it last week, and now it works
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks
|

03-21-07, 02:24 PM
|
 |
Community VIP
|
|
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes, or go to http://www.w3schools.com
There's some pages on AJAX.
__________________
Jack Bauer makes Chuck Norris cry
|

03-22-07, 02:49 PM
|
|
Wannabe Coder
|
|
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
All i want to query mysql like this select ip,time from visitor and then output the result in to page and make frequent call to php so i get updated data without refresh. Unfortunely non of the tutorial i found deals with this!! I even do know what should be the format of xml before even trying to display it!! :-(
Last edited by method; 03-22-07 at 03:11 PM.
|

03-22-07, 05:22 PM
|
 |
Level II Curmudgeon
|
|
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
|
|
Quote:
Originally Posted by UnrealEd
search google for "AJAX and PHP", that's how i did it last week, and now it works 
|
I'm going to patent that whacky "search for stuff" idea.
|

03-22-07, 05:25 PM
|
 |
Level II Curmudgeon
|
|
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
|
|
Quote:
Originally Posted by method
All i want to query mysql like this select ip,time from visitor and then output the result in to page and make frequent call to php so i get updated data without refresh. Unfortunely non of the tutorial i found deals with this!! I even do know what should be the format of xml before even trying to display it!! :-(
|
Try this tiny AJAX package:
http://crossbrowserajax.com/
Set your javascript call in a setTimeout() loop and have at it.
|

03-23-07, 05:25 AM
|
 |
Community VIP
|
|
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
AJAX isn't very difficult.
First, you make your PHP page to select the data you want from the database.
Then you make the page where you want to display these datas.
On that page, you write some Javascript that calls the PHP page you created first. There are hundreds of tutorials that show you how to call a PHP page, and output their data. Just read some of the tutorials, and get working.
You can't expect us to code everything from scratch for you.
__________________
Jack Bauer makes Chuck Norris cry
|

03-23-07, 05:29 AM
|
|
Wannabe Coder
|
|
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ii don't know how to get data from mysql and display it. All the examples i found in google they deal with diffrent type of data retrive and display. Non of those tutorials actually teach data display. I wish my data lookedd like them!!!
for example some of them make php request and just display time !!!
While i want them to make get request and get data from php. Like in my php i want to run a query like this select ip,date from visitor then send the output back to javascrpit to it display it. I want to make this request frequenly some how so i have updated data!
Last edited by method; 03-23-07 at 05:40 AM.
|

03-23-07, 05:59 AM
|
 |
Community Liaison
|
|
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
|
|
Quote:
Originally Posted by method
While i want them to make get request and get data from php. Like in my php i want to run a query like this select ip,date from visitor then send the output back to javascrpit to it display it. I want to make this request frequenly some how so i have updated data!
|
then search the web for "PHP mysql tutorial"
sorry about me always telling you to search on the web, but these are things you can find on the web very easily. There are plenty of websites about that.
To pass the data to Javascript, use the responseXML field of XMLHttpRequest class. In php, when you loop over the fields you selected from the database, you display xml. Here's a small example:
this will return a nice and clean xml, even when there's an error
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks
|

03-23-07, 06:36 AM
|
|
Wannabe Coder
|
|
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
unreal thanks.could you help me with the javascript part. I just want it to output the fetched data to page. I do not know how to make it work with the type of xml that your php created. All i want to display each records in one seperate line below each other. i can not understand how dataFetched function works
One more question. inorder that javascript gets data from mysql the data ALways should be in form of XML or is there any other way too?
Javascript Code:
<script> <!-- var REFRESH_TIME = 25000; var requester; var timerId; function getElementById(id) { if (document.all) { return document.all(id); } else if (document.getElementById) { return document.getElementById(id); } } function WindowLoad(func) { var prev=window.onload; window.onload=function(){ if(prev)prev(); func(); } } function WindowUnload(func) { var prev = window.onunload; window.onunload=function() { if(prev)prev(); func(); } } WindowLoad(init); WindowUnload(shutdown); function init() { onTimerFired(); } function shutdown() { clearTimeout(timerId); } function onTimerFired() { fetchData("http://localhost/getdata.php"); timerId = self.setTimeout("onTimerFired()", REFRESH_TIME); } function fetchData(url) { if(window.XMLHttpRequest) { try { requester = new XMLHttpRequest(); } catch(e) { requester = false; } } else if (window.ActiveXObject) { try { requester = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { requester = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { requester = false; } } } if (requester) { requester.onreadystatechange=dataFetched; requester.open("GET", url); requester.send(null); } } [B]function dataFetched() { if (requester.readyState == 4) { if (requester.status == 200) { root = requester.responseXML.getElementsByTagName("playing").item(0); var artist = root.getElementsByTagName("artist").item(0).firstChild.nodeValue; var song = root.getElementsByTagName("song").item(0).firstChild.nodeValue; var image = root.getElementsByTagName("image").item(0).firstChild.nodeValue; var rating = root.getElementsByTagName("rating").item(0).firstChild.nodeValue; var songid = root.getElementsByTagName("songid").item(0).firstChild.nodeValue; getElementById("RJSong").innerHTML = artist + " - " + song; getElementById("RJImage").innerHTML = "<img src=\"" + image + "\" width=\"40\" height=\"40\" border=\"0\"/>"; } } }[/B] // --> </script>
Last edited by UnrealEd; 03-23-07 at 07:11 AM.
Reason: please use the [highlight=Javascript] wrapper when posting Javascript code
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|