How To Get Mysql Data with Ajax ?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdoelmasry
    New Member
    • Oct 2006
    • 104

    How To Get Mysql Data with Ajax ?!

    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • abdoelmasry
      New Member
      • Oct 2006
      • 104

      #3
      Thank U acoder

      I got it

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You're welcome.

        Can you post your working code here for the benefit of others? Thanks.

        Comment

        • abdoelmasry
          New Member
          • Oct 2006
          • 104

          #5
          Here is my code :

          Javascript code:

          Code:
          function createajaxrequest(){
          var req;
          if(window.XMLHttpRequest){
          req = new XMLHttpRequest();
          } else if(window.ActiveXObject) {
          req = new ActiveXObject("Microsoft.XMLHTTP");
          } else {
          alert('Error Creating Ajax Request');
          exit;
          }
          return req;
          }
          
          function getphotoslist(){
          var serverpage="getphotos.php?action=refreshphotos";
          xmlhttp = createajaxrequest();
          xmlhttp.open("get",serverpage);
          xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          photosdiv.innerHTML = xmlhttp.responseText;
          }
          }
          xmlhttp.send(null);
          getphotosloop = setTimeout("getphotoslist()",2000);
          }
          Html Page:

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

          getphotos.php Code :
          [PHP]<?php

          if($_GET["action"]=="refreshphoto s"){

          function getphotoslist() {
          $gtdata=mysql_q uery("select id,name,path,ti me,date,postby, pcip from photos");
          while($cdata=my sql_ftch_row($g tdata)){
          $photoinfo[$cdata[0]]=array("id"=>"$ cdata[0]","name"=>"$cda ta[1]","path"=>"$cda ta[2]","time"=>"$cda ta[3]","date"=>"$cda ta[4]","postby"=>"$c data[5]","pcip"=>"$cda ta[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=getphot oslist();
          $phlistcode=get photoscode($phl ist);
          echo $phlistcode;
          exit;
          }
          ?>[/PHP]

          Greeting

          Comment

          Working...