Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form with drop-down options, when the user selects an option AJAX should dynamically update the page with data from my MySQL database using PHP.

The page with the form on can be found here.

Selecting an option you can see the current error messages.

The database is called "products" and the table within the database which I'm trying to access is called "deepblack"

Database:

id,width,length,price
1,2,5,12
2,2,10,20
3,3,5,18
4,3,10,30
5,4,5,24
6,4,10,40

The Markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">2-5</option>
<option value="2">2-10</option>
<option value="3">3-5</option>
<option value="4">3-10</option>
<option value="5">4-5</option>
<option value="6">4-10</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

PHP - getuser.php

<?php
$q = intval($_GET['q']);

$con = mysql_connect('cust-mysql-123-17','products','abc123','products');
if (!$con)
  {
  die('Could not connect: ' . mysql_error($con));
  }

mysql_select_db($con,"products");
$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($con,$sql);

echo "<table border='1'>
<tr>
<th>id</th>
<th>width</th>
<th>length</th>
<th>price</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['width'] . "</td>";
  echo "<td>" . $row['length'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Does anyone know why I am getting these error messages and what needs to be changed?

Thanks

share|improve this question
add comment

3 Answers

I think you inversed the parameters of mysql_select_db in your PHP Code. Try this instead:

mysql_select_db('products', $con);

Same for mysql_query:

$result = mysql_query($sql, $con);
share|improve this answer
add comment

If you open your web inspector and go to the network tab and then the xhr section you'll see that the request URL is:

http://www.projector-screen-material.co.uk/blah/getuser.php3
http://www.projector-screen-material.co.uk/blah/getuser.php4
http://www.projector-screen-material.co.uk/blah/getuser.php5

I can think of two things that are going wrong:

Your php file is not in the blah directory and you actually want /get_userphp instead of just get_user.php.

It looks like you are concatenating your requests incorrectly which is turning:

http://www.projector-screen-material.co.uk/blah/getuser.php?q=5

into

http://www.projector-screen-material.co.uk/blah/getuser.php5
share|improve this answer
add comment

In actual code on the website you have:

xmlhttp.open("GET","getuser.php"+str,true);

?q= missing

code here is correct, just upload it to your server

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.