Jump to content

Recommended Posts

Hi i want to retrieve data from mysql database using ajax and php. my code is below which does not work

 

here is index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
var url = "getCustomerData.php?id=";
var xhr = false;

if(window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

function handleHttpResponse() 
{   
        if (xhr.readyState == 4 && xhr.status == 200) 
	{
              var results=xhr.responseText;
              document.getElementById('divCustomerInfo').innerHTML = results;
        }
}

  function requestCustomerInfo() 
  {     
            var sId = document.getElementById("txtCustomerId").value;
            xhr.open("GET", url + escape(sId), true);
            xhr.onreadystatechange = handleHttpResponse;
            xhr.send(null);
  } 
</script>
</head>

<body>
<p>Enter Customer ID to retriew Information.</p>
<form method="post" action="getCustomerData.php">
<p>Customer ID: <input type="text" id="txtCustomerid" value="" /></p>
<p><input type="button" value="Get Customer Info" onclick="requestCustomerInfo()" /></p>
</form>
<div id="divCustomerInfo"></div>
</body>
</html>

 

here is display.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$id = $_GET['id'];
echo $id;

$connect = mysql_connect("localhost","user","pass");
mysql_select_db("customers");
$query = "select * from customer where id=$id";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
echo $row['name']."<br>";
echo $row['address']."<br>";
echo $row['phone'];
}
?> 
</body>
</html>

 

please help

Here is one thing wrong:

 

In this function you have txtCustomerId where id is with the "I" in uppercase.

function requestCustomerInfo() 
{     
    
    var sId = document.getElementById("txtCustomerId").value;
    console.log(sId);
    xhr.open("GET", url + escape(sId), true);
    xhr.onreadystatechange = handleHttpResponse;
    xhr.send(null);
} 

 

in your embedded onclick event you have txtCustomerid where id is with the "i" in lowercase.

<input type="button" value="Get Customer Info" onclick="requestCustomerInfo()" />

 

Use a javascript debugger to find these kind of typos

In addition to the I/i issue mentioned in the last post, then you might want to consider what your display.php outputs.  You have it outputting full HTML with DOCTYPE head, body etc, when all you really need in your div, is the database output you are echoing.

 

So, as this is a php file to be called by ajax to get some server side info and return it, then it does not need all the HTML around it.

 

the whole file can be :

<?php

$id = $_GET['id'];

echo $id;

 

$connect = mysql_connect("localhost","user","pass");

mysql_select_db("customers");

$query = "select * from customer where id=$id";

$result = mysql_query($query);

 

while($row = mysql_fetch_assoc($result))

{

  echo $row['name']."<br>";

  echo $row['address']."<br>";

  echo $row['phone'];

}

?>

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.