I'm new to MySQL & would like to "filter" a result set by region. I have previously asked a similar question but I don't know if I'm meant to create a new question or continue that one, sorry if I've got this wrong.
I've looked at the http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_database & think it would be ideal, but I'm not sure how to get it working, or how I can get it to "update" the results.
Ideally I suppose a drop down box below the region would look tidy - but, I'm not having any success - I really am totally limited to my understanding, can anyone help? (thank you to previous folks who helped with the first part too!!)
This is all I have so far, (to give an idea of what I'd like to filter).
Many thanks, scotia - below is the regionbox.php file
...
<script type="text/javascript">
function selectRegion(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("region").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("region").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","regionbox.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<P>
<table class="dbTable">
<tr>
<tr><th>Commodity</th> <th><form action="regionbox.php">
<select name="region" onchange="selectRegion(this.value)">
<option value="">Select Region</option>
<option value="E. Midlands">E. Midlands</option>
<option value="Gtr. London">Gtr. London</option>
<option value="North East">North East</option>
<option value="North West">North West</option>
<option value="Scotland">Scotland</option>
<option value="South East">South East</option>
<option value="South West">South West</option>
<option value="W. Midlands">W. Midlands</option>
<option value="Wales">Wales</option>
</select>
</form></th> <th>Member</th> <th>Size</th> <th>Price</th> <th>Date Posted</th>
</tr>
<?php
$link = mysql_connect('localhost', '', '');
$db_selected = mysql_select_db('palegall_newTrader', $link);
if (!$db_selected) {
die ('cant find newTrader' . mysql_error());
}
$region = mysql_real_escape_string($_POST['region']);
$query = mysql_query("SELECT * FROM `sell` WHERE `commodity` = 'paper' ORDER BY `price`")
or die( mysql_error() );
echo '<table class="dbTable">';
while ($row = mysql_fetch_assoc($query))
{
echo '<tr><td>'.$row['commodity'].'</td> <td>'.$row['region'].'</td> <td>'.
$row['member'].'</td> <td>'.$row['size'].'</td> <td>'.
$row['price'].'</td> <td>'.$row['posted'].'</td> </tr>';
}
echo "</table>";
?>
</body></html>
I've stripped out bits. I hope this is OK.
regionbox.phpin order to answer your question, please remove the link in order to not have people hack your site.CTRL+Kto make it look like code.