Ajax and php
Posted: Wed Sep 29, 2010 3:14 pm
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:
getproducts.php:
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
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>
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);
?> 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