'PHP and AJAX MySQL Database Example'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jafarsalam
    New Member
    • Feb 2007
    • 1

    'PHP and AJAX MySQL Database Example'

    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.c om/php/php_ajax_databa se.asp

    I downloaded the code and uploaded to my server but it does not work . I script does not display the data from the database.

    Can anyone look at this and help me out.

    thanks
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    If you create the user table in ajax_demo database definitely it will work.

    Why don't you post the Coding here, that you used in your server.

    Comment

    • bluez
      New Member
      • Apr 2007
      • 5

      #3
      Me too... I keep getting error message from the HTML page

      Line : 2
      Char : 21
      Error : Expected ';'

      Line : 7
      Char : 1
      Error : Object Expected

      =============== =============== =============== =
      [PHP]<html>
      <head>
      <script src="selectuser .js"></script>
      </head>
      <body><form>
      Select a User:
      <select name="users" onchange="showU ser(this.value) ">
      <option value="1">20000 01</option>
      <option value="2">20000 02</option>
      <option value="3">20000 03</option>
      <option value="4">20000 04</option>
      </select>
      </form><p>
      <div id="txtHint"><b >User info will be listed here.</b></div>
      </p></body>
      </html>[/PHP]

      Im really no idea
      Thanks
      Last edited by Atli; Sep 20 '08, 01:22 PM. Reason: Added [code] tags.

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by bluez
        Me too... I keep getting error message from the HTML page

        Line : 2
        Char : 21
        Error : Expected ';'

        Line : 7
        Char : 1
        Error : Object Expected

        =============== =============== =============== =
        <html>
        <head>
        <script src="selectuser .js"></script>
        </head>
        <body><form>
        Select a User:
        <select name="users" onchange="showU ser(this.value) ">
        <option value="1">20000 01</option>
        <option value="2">20000 02</option>
        <option value="3">20000 03</option>
        <option value="4">20000 04</option>
        </select>
        </form><p>
        <div id="txtHint"><b >User info will be listed here.</b></div>
        </p></body>
        </html>

        Im really no idea
        Thanks
        This Error is Coming from your selectuser.js file. You are posting only HTML part.
        Try this and if not working post the js file. Note the " ; " by the end of function.
        Code:
        <select name="users" onchange="showUser(this.value);">

        Comment

        • bluez
          New Member
          • Apr 2007
          • 5

          #5
          Here the JavaScript and PHP files. Im only did some changes on PHP file.

          selectuser.js
          Code:
          var xmlHttpfunction showUser(str)
          { 
          xmlHttp=GetXmlHttpObject()
          if (xmlHttp==null)
           {
           alert ("Browser does not support HTTP Request")
           return
           } 
          var url="getuser.php"
          url=url+"?q="+str
          url=url+"&sid="+Math.random()
          xmlHttp.onreadystatechange=stateChanged 
          xmlHttp.open("GET",url,true)
          xmlHttp.send(null)
          }
          
          function stateChanged() 
          { 
          if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
           { 
           document.getElementById("txtHint").innerHTML=xmlHttp.responseText
           } 
          }function GetXmlHttpObject()
          {
          var xmlHttp=null;
          try
           {
           // Firefox, Opera 8.0+, Safari
           xmlHttp=new XMLHttpRequest();
           }
          catch (e)
           {
           //Internet Explorer
           try
            {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
           catch (e)
            {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
           }
          return xmlHttp;
          }
          getuser.php
          Code:
          <?php
          
          include("Database/database.php");
          
          global $conn;
          
          $q=$_GET["q"];
          
          $sql="SELECT * FROM student_recording WHERE Stu_ID = $q";
          
          $result = $conn->query($sql);
          
          echo "<table border='1'>
          <tr>
          <th>Firstname</th>
          <th>Class Code</th>
          <th>Race</th>
          </tr>";
          
          while($row = $result->fetch_assoc())
           {
           echo "<tr>";
           echo "<td>" . $row['Stu_Name'] . "</td>";
           echo "<td>" . $row['Class_Code'] . "</td>";
           echo "<td>" . $row['Stu_Race'] . "</td>";
           echo "</tr>";
           }
          echo "</table>";
          
          ?>

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Originally posted by bluez
            Here the JavaScript and PHP files. Im only did some changes on PHP file.
            I think both of you are using This Sample Application.
            jafarsalam,
            If this is not your problem please post a comment on that.

            bluez,
            Try this Coding.Note that you have merged xmlHttp variable and the ShowUSer function in your JS file.
            And the ; Missing by end of xmlHttp Variable Declaration.

            Code:
            var xmlHttp;
            
            function showUser(str)
            { 
            xmlHttp=GetXmlHttpObject()
            if (xmlHttp==null)
             {
             alert ("Browser does not support HTTP Request")
             return
             } 
            var url="getuser.php"
            url=url+"?q="+str
            url=url+"&sid="+Math.random()
            xmlHttp.onreadystatechange=stateChanged 
            xmlHttp.open("GET",url,true)
            xmlHttp.send(null)
            }
            
            function stateChanged() 
            { 
            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
             { 
             document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
             } 
            }
            
            function GetXmlHttpObject()
            {
            var xmlHttp=null;
            try
             {
             // Firefox, Opera 8.0+, Safari
             xmlHttp=new XMLHttpRequest();
             }
            catch (e)
             {
             //Internet Explorer
             try
              {
              xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
              }
             catch (e)
              {
              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
             }
            return xmlHttp;
            }

            Comment

            • bluez
              New Member
              • Apr 2007
              • 5

              #7
              Thanks ajaxrand...

              It's work now. Sorry for never check it properly :)

              Comment

              Working...