Page 1 of 1

Ajax and php

Posted: Wed Sep 29, 2010 3:14 pm
by KoenVH
Hello,

I'm pretty new to web developpement but am trying to make a script work.
i have 2 pages:
products.php and getproduct.php
products.php:

Code: Select all

<!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>
<title>Producten</title>
<LINk HREF="style.css" REL="stylesheet" TYPE="text/css">
<script type="text/javascript">
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","getProduct.php?q="+str,true);
xmlhttp.send();
}
</script>

</head>

<body>
<!--Openen van de dataontainer-->
<div id="container">

<!--Gedeelte Blog-->
	<div id="supportingText">
		<div id="header"></div>
		<div>			
<?php 
			?>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a product group:</option>

<?php
			include ("includes/dbconn.inc");
			$query = "SELECT * FROM tblProdGroep ORDER BY Name Asc";
			$result = mysql_query($query,$connection);
			$x = 0;
			while ($row = mysql_fetch_array($result))
			{
			extract($row);
			x += 1;
			echo "<option value='".$x."'>".$Name."</option>";
			}
?>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
			
		</div>
		<div id="footer"></div>
	</div>
	<br />
	<?php
	}
	?>	
</div>			

</body>
</html>
getproducts.php:

Code: Select all

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'name', 'password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("poolOps", $con);

$sql="SELECT * FROM tblProducten WHERE Name = '".$q."'";

$result = mysql_query($sql);

echo $q.": 
<br>
<table border='1'>
<tr>
<th>Product</th>
<th></th>
<th>Description</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td><img src='afb/"  . $row['Picture'] . " width='100' height='100' align='middle' /></td>";
  echo "<td>" . $row['Description'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 
I cant get this to work.
The purpose is to get data into a dropdown and when an item is selected it displays the information about this item.

Can anybody help me plz?

Thx in advance.

KoenVH

Re: Ajax and php

Posted: Sat Oct 02, 2010 7:17 pm
by JakeJ
I'd like to make a suggestion. Use jQuery and the form plug in. That will handle all the ajax stuff for you. It's pretty cool really. And the learning curve is pretty small.

Move your include for the database connection outside the form tags also. It's just good practice but it can produce unintended results.

I can't remember what it was, but recently, I had something between my form tags recently that cause everything after this piece of code to be invisible. It showed up in the page source but it didn't render.