Current location: Hot Scripts Forums » General Web Coding » JavaScript » How to recive mysql data using javascript ajax?


How to recive mysql data using javascript ajax?

Reply
  #1 (permalink)  
Old 03-21-07, 01:13 PM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow 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
Reply With Quote
  #2 (permalink)  
Old 03-21-07, 01:19 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
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

Reply With Quote
  #3 (permalink)  
Old 03-21-07, 02:24 PM
Vicious's Avatar
Vicious Vicious is offline
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
Reply With Quote
  #4 (permalink)  
Old 03-22-07, 02:49 PM
method method is offline
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.
Reply With Quote
  #5 (permalink)  
Old 03-22-07, 05:22 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by UnrealEd View Post
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.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #6 (permalink)  
Old 03-22-07, 05:25 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by method View Post
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.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #7 (permalink)  
Old 03-23-07, 05:25 AM
Vicious's Avatar
Vicious Vicious is offline
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
Reply With Quote
  #8 (permalink)  
Old 03-23-07, 05:29 AM
method method is offline
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.
Reply With Quote
  #9 (permalink)  
Old 03-23-07, 05:59 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
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 View Post
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:
PHP Code:

// this is necessary, otherwise it won't work:

header('Content-type: application/xml');
// you need to return the error as xml as well
$res mysql_query('SELECT * FROM mytable') or die('<error>'.mysql_error().'</error>');
// display the root node of the xml, and start looping over the elements:
echo '<data>';
while(
$row mysql_fetch_row($res)){
  echo 
'<row>';
  echo 
'<a>'.$row['field_1'].'</a>';
  echo 
'<b>'.$row['field_2'].'</b>';
  echo 
'</row>';
}
echo 
'</data>'
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

Reply With Quote
  #10 (permalink)  
Old 03-23-07, 06:36 AM
method method is offline
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:
  1. <script>
  2. <!--
  3.  
  4. var REFRESH_TIME = 25000;
  5. var requester;
  6. var timerId;
  7.  
  8. function getElementById(id)
  9. {
  10.     if (document.all)
  11.     {
  12.       return document.all(id);
  13.     }
  14.     else if (document.getElementById)
  15.     {
  16.         return document.getElementById(id);
  17.     }
  18. }
  19.  
  20. function WindowLoad(func)
  21. {
  22.     var prev=window.onload;
  23.     window.onload=function(){ if(prev)prev(); func(); }
  24. }
  25.  
  26. function WindowUnload(func)
  27. {
  28.     var prev = window.onunload;
  29.     window.onunload=function() { if(prev)prev(); func(); }
  30. }
  31.  
  32. WindowLoad(init);
  33. WindowUnload(shutdown);
  34.  
  35. function init()
  36. {
  37.     onTimerFired();
  38. }
  39.  
  40. function shutdown()
  41. {
  42.     clearTimeout(timerId);
  43. }
  44.  
  45. function onTimerFired()
  46. {
  47.     fetchData("http://localhost/getdata.php");
  48.     timerId = self.setTimeout("onTimerFired()", REFRESH_TIME);
  49. }
  50.  
  51. function fetchData(url)
  52. {
  53.     if(window.XMLHttpRequest)
  54.     {
  55.         try
  56.         {
  57.             requester = new XMLHttpRequest();
  58.         }
  59.         catch(e)
  60.         {
  61.             requester = false;
  62.         }
  63.     }
  64.     else if (window.ActiveXObject)
  65.     {
  66.         try
  67.         {
  68.             requester = new ActiveXObject("Msxml2.XMLHTTP");
  69.         }
  70.         catch(e)
  71.         {
  72.             try
  73.             {
  74.               requester = new ActiveXObject("Microsoft.XMLHTTP");
  75.             }
  76.             catch(e)
  77.             {
  78.               requester = false;
  79.             }
  80.         }
  81.     }
  82.  
  83.     if (requester)
  84.     {
  85.         requester.onreadystatechange=dataFetched;
  86.         requester.open("GET", url);
  87.         requester.send(null);
  88.     }
  89. }
  90.  
  91. [B]function dataFetched()
  92. {
  93.     if (requester.readyState == 4)
  94.     {
  95.         if (requester.status == 200)
  96.         {
  97.             root = requester.responseXML.getElementsByTagName("playing").item(0);
  98.             var artist = root.getElementsByTagName("artist").item(0).firstChild.nodeValue;
  99.             var song = root.getElementsByTagName("song").item(0).firstChild.nodeValue;
  100.             var image = root.getElementsByTagName("image").item(0).firstChild.nodeValue;
  101.             var rating = root.getElementsByTagName("rating").item(0).firstChild.nodeValue;
  102.             var songid = root.getElementsByTagName("songid").item(0).firstChild.nodeValue;           
  103.            
  104.            
  105.             getElementById("RJSong").innerHTML = artist + " - " + song;
  106.             getElementById("RJImage").innerHTML = "<img src=\"" + image + "\" width=\"40\" height=\"40\" border=\"0\"/>";
  107.         }
  108.     }
  109. }[/B]
  110.  
  111. // -->
  112. </script>

Last edited by UnrealEd; 03-23-07 at 07:11 AM. Reason: please use the [highlight=Javascript] wrapper when posting Javascript code
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
For Hire (3 years exp.): Looking for Php, Mysql, AJAX, JavaScript, HTML, XML Barkat Job Offers & Assistance 1 09-25-07 04:15 PM
How to get PHP to input data into a MYSQL table? scl789 PHP 5 04-21-05 09:06 PM
problem printing mysql data by "x rows/page" abtimoteo PHP 1 07-30-04 07:55 PM


All times are GMT -5. The time now is 11:25 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.