954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help in retreiving data from mysql using ajax

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']."";
   echo $row['address']."";
   echo $row['phone'];
}
?>
</body>
</html>


please help me

doforumda
Newbie Poster
8 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Firstly, "Does not work" is very unspecific. Please explain what it's doing and what it's supposed to do and you're more likely to get an explaination on what your problem is.

Secondly, I don't think content put in a webpage with element.innerHTML should have tags. My suggestion would be to remove everything outside of the <?php ?> tag from display.php.

Thirdly, which files' contents did you paste? You mentioned display.php in your post, but your code refers to getCustomerData.php.

Fourthly, you shouldn't use the same file for AJAX requests and form actions. AJAX responses should be short and simple, while form actions usually include some headers and footers. You can include() the AJAX page from the form page, but don't put anything pointless (like tags) in the AJAX one.

Alcaro
Newbie Poster
4 posts since Jan 2010
Reputation Points: 8
Solved Threads: 0
 

Hello Sir,

I am PHP Mysql Freelancer, I go through your problems! Its becauase you have Head, Body and HTML tags from Dsipaly.php file! And it will work out for sure!
If you may still have problems! Then contact me i will solve it for you!

Please check few of my work below!

Free Links Directory
A Indian Mufti store To Promote Your Business
Website Design Company India
Free Hot Scripts, PHP Scripts Website
Programming Discussion Forum for Developers
Web Development Work Faridabad
Future Endeavour IT Training Center
Social Networking Website for Indians Community


Please go through these websites and tell me if i can get some work of yours!

Regardds,

Freelance / Freelancing Programmer

freelancerquest
Newbie Poster
4 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Thanks for your sharing. Its really useful and informative.

social engine customization

rajanrufus
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You