Jump to content

Retrieving Data from MySQL using PHP and Ajax


lilman

Recommended Posts

I found this site with a pretty simple tutorial, however; it does not work.  Below is the exact code I am using (except for db info), and is the exact code I got from the tutorial.  The problem I am having is that I type in the CustomerID and it tells me no matches have been found, when in fact, there should be.  For example, I will put in 1 which there is a CustomerID that has 1 and it tells me no matches.  If someone could tell me why this isn't working, I'd really appreciate it.

 

GetCustomerData.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>Get Customer Data</title>
<?php
   
    //customer ID
    $sID = $_GET["id"];
   
    //variable to hold customer info
    $sInfo = "";
   
    //database information
    $sDBServer = "your_server_name";
    $sDBName = "your_database_name";
    $sDBUsername = "your_user_name";
    $sDBPassword = "your_password";

    //create the SQL query string
    $sQuery = "Select * from Customers where CustomerId=".$sID;
             
    //make the database connection
    $oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
    @mysql_select_db($sDBName) or $sInfo = "Unable to open database";
       
    if($sInfo == '') {
        if($oResult = mysql_query($sQuery) and mysql_num_rows($oResult) > 0) {
            $aValues = mysql_fetch_array($oResult,MYSQL_ASSOC);
            $sInfo = $aValues['Name']."<br />".$aValues['Address']."<br />".
                     $aValues['City']."<br />".$aValues['State']."<br />".
                     $aValues['Zip']."<br /><br />Phone: ".$aValues['Phone']."<br />".
                     "<a href=\"mailto:".$aValues['E-mail']."\">".$aValues['E-mail']."</a>";
        } else {
            $sInfo = "Customer with ID $sID doesn't exist.";
        }
    }
   
    mysql_close($oLink);

?>
   
   

</head>
<body>
    <div id="divInfoToReturn"> <?php echo $sInfo ?> </div>
</body>
</html>

 

display.htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>Customer Account Information</title>
    <script type="text/javascript">
        var url = "GetCustomerData.php?id="; // The server-side script
       function handleHttpResponse() {   
        if (http.readyState == 4) {
              if(http.status==200) {
                  var results=http.responseText;
              document.getElementById('divCustomerInfo').innerHTML = results;
              }
              }
        }
       
        function requestCustomerInfo() {     
            var sId = document.getElementById("txtCustomerId").value;
            http.open("GET", url + escape(sId), true);
            http.onreadystatechange = handleHttpResponse;
            http.send(null);
        }
function getHTTPObject() {
  var xmlhttp;

  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
   
}
  return xmlhttp;


}
var http = getHTTPObject(); // We create the HTTP Object
</script>
</head>
<body>
    <p>Enter customer ID number to retrieve information:</p>
    <p>Customer ID: <input type="text" id="txtCustomerId" value="" /></p>
    <p><input type="button" value="Get Customer Info" onclick="requestCustomerInfo()" /></p>
    <div id="divCustomerInfo"></div>
</body>
</html>

 

customers.txt

-- phpMyAdmin SQL Dump
-- version 2.6.0-pl3
-- 
-- Host: localhost
-- Generation Time: Apr 30, 2006 at 05:45 PM
-- Server version: 4.1.8
-- PHP Version: 5.0.3
-- 
-- Database: `ajax_ex`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `customers`
-- 

CREATE TABLE `customers` (
  `CustomerId` int(11) NOT NULL auto_increment,
  `Name` varchar(255) NOT NULL default '',
  `Address` varchar(255) NOT NULL default '',
  `City` varchar(255) NOT NULL default '',
  `State` varchar(255) NOT NULL default '',
  `Zip` varchar(255) NOT NULL default '',
  `Phone` varchar(255) NOT NULL default '',
  `E-mail` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`CustomerId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Sample Customer Data';

-- 
-- Dumping data for table `customers`
-- 

INSERT INTO `customers` VALUES (1, 'Suraj Thapliya', 'Naxal, Bhagbati', 'Kathmandu', 'KTM', '0977', '977-9803165329', 'surajthapaliya@gmail.com');
INSERT INTO `customers` VALUES (2, 'Birijan Maharjan', 'Kathmandu', 'Kathmandu', 'Ktm', '977', '9803020016', 'birijan@wlink.com.np');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.