Hi, I am a PHP newbie and need some help with this storelocator I am building.
I would like to user to select the county they would like to find store located in by using a drop down menu.
this is the html and AJAX for the user interface
I have a table called store in mySQL db with the following fields: company_name, address, county, country, phone, web_site.HTML Code:<html> <head> <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","getuser.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> <select name="shop" onchange="showUser(this.value)"> <option value="">Select a County/Region:</option> <option value="1">West Midlands</option> <option value="2">East Midlands</option> <option value="3">South West</option> <option value="4">South East</option> <option value="5">The North</option> <option value="6">Scotland</option> <option value="7">Wales</option> <option value="8">London</option> <option value="9">Herefordshire</option> </select> </form> <br /> <div id="txtHint"><b>Try one of these outlets or shops</b></div> </body> </html>
I would like the user to be able to select e.g. "Herefordshire" and all shops with county = Herefordshire should be displayed. This is the page for the php operation:
The above doesn't pull any data - which I assume it because I don't know how to set up the query properly.PHP Code:<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'shops', 'pwds123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("shops", $con);
$sql="SELECT * FROM shop WHERE county = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Company Name</th>
<th>Address</th>
<th>City/Town</th>
<th>County</th>
<th>Post Code</th>
<th>Phone</th>
<th>Web Site</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['city_town'] . "</td>";
echo "<td>" . $row['county'] . "</td>";
echo "<td>" . $row['post_code'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['web_site'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The reason I am using AJAX on the html script is because I found an example on the web but I realise it might not be the best way but am not capable to determine that.
Appreciate any help or nudge in the right direction. As I said, am a newbie so need some detailed help.


Reply With Quote

Bookmarks