473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How To Get Mysql Data with Ajax ?!

abdoelmasry
104 100+
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
4 2377
acoder
16,027 Expert Mod 8TB
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
104 100+
Thank U acoder

I got it
May 19 '08 #3
acoder
16,027 Expert Mod 8TB
You're welcome.

Can you post your working code here for the benefit of others? Thanks.
May 19 '08 #4
abdoelmasry
104 100+
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

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

Similar topics

7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
3
by: emrahayanoglu | last post by:
Hello Everyone, I have question about efficient usage of connecting to mysql. Now, i'm using 6 or 7 queries in a page with ajax. In Addition to that, i open connection to mysql for every query....
4
by: d3vkit | last post by:
Okay so I am at a loss here. I have a website that I've previously had no trouble connecting to the mysql DB on. I have an include to a connect file with the relevant connection info, and it was...
1
by: bcochofel | last post by:
Hi, I'm using Perl CGI and HTML::Template to generate the following XML: ---------------------------------------------------------------------- <?xml version="1.0" encoding="iso-8859-1"?>...
6
jafarsalam
by: jafarsalam | last post by:
hi; I'm new to PHP and AJAX MySQL codes. I found a simple code for PHP and AJAX MySQL Database communication on : http://www.w3schools.com/php/php_ajax_database.asp I downloaded the code and...
0
by: tusaar | last post by:
Hi all I am in big need for a drop down menu created with php, mysql and ajax. Exactly, I need three drop down menu (Category, Subcategory and Item). The data of each drop down will come from...
3
by: jarremw | last post by:
hello all, what i have is a modal popup control extender, i have an ajax script that saves the value of the two textboxes that are in the popup, what i am needing is a way to insert those values into...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
7
angelhiso
by: angelhiso | last post by:
hei coild somebody help me with ajax mysql ? I use ajax to put data to my database but why i can not put the charector like ' \ ? for example i want to put the word DON'T which has the 'it could not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.