I have a current set up of being able to select a minimum price to display from, and the page will update accordingly with no issues. However, I also want to be able to set a maximum price to display from a different <select> (and in future, other options such as product types too). However, the method that I am using (http://www.w3schools.com/php/php_ajax_database.asp) does not work when I modify it to pass through 2 values, it will not display any results. I think it is failing when trying to actually get hold of the maximum value, but I do not know at which point (or why) this is failing. Any help would be greatly appreciated! I have spent quite some time with trial and error, and nothing else tutorial-wise has been of much use. If you are able to help, please explain your answer as I am new to AJAX/JS in general.
HTML:
<select id="price-from" onchange="setPrice(this.value)">
<option selected value="500">£500</option>
<option value="1000">£1,000</option>
<option value="2000">£2,000</option>
<option value="3000">£3,000</option>
<option value="4000">£4,000</option>
<option value="5000">£5,000</option>
<option value="6000">£6,000</option>
<option value="7000">£7,000</option>
<option value="8000">£8,000</option>
<option value="9000">£9,000</option>
<option value="10000">£10,000</option>
<option value="20000">£20,000</option>
</select>
<p id="to">To</p>
<select id="price-to" onchange="setPrice(this.value)">
<option value="500">£500</option>
<option value="1000">£1,000</option>
<option value="2000">£2,000</option>
<option value="3000">£3,000</option>
<option value="4000">£4,000</option>
<option value="5000">£5,000</option>
<option value="6000">£6,000</option>
<option value="7000">£7,000</option>
<option value="8000">£8,000</option>
<option value="9000">£9,000</option>
<option value="10000">£10,000</option>
<option selected value="20000">£20,000</option>
</select>
JavaScript:
function setPrice(str) {
if (str == "") {
document.getElementById("section1").innerHTML = "<p>No Results</p>";
return;
alert("error 1");
} else {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("section1").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","priceQuery.php?min=" +str + "max=" +str, true);
xmlhttp.send();
}
}
PHP:
<?php
$con = mysqli_connect('localhost','root','','nbgardens');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"nbgardens");
$min = (isset($_GET['min']) ? $_GET['min'] : null);
$max = (isset($_GET['max']) ? $_GET['max'] : null);
$sql="SELECT * FROM products WHERE product_price >= '".$min."' AND product_price<= '".$max."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result)){
echo"<div class='browse-image'>
<a href='#'>
<p class='price'>£" . $row['product_price'] . "</p>
<img src='" . $row['product_img'] . "' width='200px' class='thumb'>
<figcaption>" . $row['product_title'] . "</figcaption></a></div>";
}
mysqli_close($con);
?>