By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,036 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,036 IT Pros & Developers. It's quick & easy.

How To Get Mysql Data with Ajax ?!

abdoelmasry
100+
P: 104
Hi Prof's

im new in ajax

i need some help

i wanna create active web page linked to mysql database

i mean .. i open my page , it will automatically view database contents

it will also get any changes in databse

like if any row deleted or any row created

i will make javascript timer to call function that get mysql data every 1 second

my last mean ... How to get Mysql Data With Ajax ???

i need some ideas

Thanks
May 15 '08 #1
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
First, get your server-side script working which connects to the MySQL database and outputs the data.

Once you have that working, it's simply a case of calling the script using Ajax. See an example.
May 15 '08 #2

abdoelmasry
100+
P: 104
Thank U acoder

I got it
May 19 '08 #3

acoder
Expert Mod 15k+
P: 16,027
You're welcome.

Can you post your working code here for the benefit of others? Thanks.
May 19 '08 #4

abdoelmasry
100+
P: 104
Here is my code :

Javascript code:

Expand|Select|Wrap|Line Numbers
  1. function createajaxrequest(){
  2. var req;
  3. if(window.XMLHttpRequest){
  4. req = new XMLHttpRequest();
  5. } else if(window.ActiveXObject) {
  6. req = new ActiveXObject("Microsoft.XMLHTTP");
  7. } else {
  8. alert('Error Creating Ajax Request');
  9. exit;
  10. }
  11. return req;
  12. }
  13.  
  14. function getphotoslist(){
  15. var serverpage="getphotos.php?action=refreshphotos";
  16. xmlhttp = createajaxrequest();
  17. xmlhttp.open("get",serverpage);
  18. xmlhttp.onreadystatechange = function() {
  19. if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  20. photosdiv.innerHTML = xmlhttp.responseText;
  21. }
  22. }
  23. xmlhttp.send(null);
  24. getphotosloop = setTimeout("getphotoslist()",2000);
  25. }
Html Page:

[HTML]<html>
<head>
<title> </title>
</head>
<body>
<div id="photosdiv"></div>
</body>
</html>[/HTML]

getphotos.php Code :
[PHP]<?php

if($_GET["action"]=="refreshphotos"){

function getphotoslist(){
$gtdata=mysql_query("select id,name,path,time,date,postby,pcip from photos");
while($cdata=mysql_ftch_row($gtdata)){
$photoinfo[$cdata[0]]=array("id"=>"$cdata[0]","name"=>"$cdata[1]","path"=>"$cdata[2]","time"=>"$cdata[3]","date"=>"$cdata[4]","postby"=>"$cdata[5]","pcip"=>"$cdata[6]");
}
return $photoinfo;
}

function getphotoscode($photosarray){
while(list($key,$value)=each($photosarray)){
$itemcode="id->$photosarray[$key][id] :: name->$photosarray[$key][name] :: path->$photosarray[$key][path] :: time->$photosarray[$key][time] :: date->$photosarray[$key][date] :: postby->$photosarray[$key][postedby] :: pcip->$photosarray[$key][pcip]";
$listcode.="<br>.$itemcode";
}
return $listcode;
}

$phlist=getphotoslist();
$phlistcode=getphotoscode($phlist);
echo $phlistcode;
exit;
}
?>[/PHP]

Greeting
Jul 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.