I want to filter data on the database using a set of ranges. I have my checkbox down below.
<body>
<form id="form" method="post" action="">
<input type="checkbox" name="price1" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $0 - 5<br>
<input type="checkbox" name="price2" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $6 - 10<br>
<input type="checkbox" name="price3" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $11 - 20<br>
</form>
</body>
<script type="text/javascript">
$(function(){
$('.checkbox').on('change',function(){
$('#form').submit();
});
});
</script>
Below is what I have for my PHP code
<?php
if (isset($_POST["price1"])){
$query = $conn->query("SELECT * FROM item WHERE price BETWEEN 0 AND 5");
}
if (isset($_POST["price2"])){
$query = $conn->queryn("SELECT * FROM item WHERE price BETWEEN 6 AND 10");
}
if (isset($_POST["price3"])){
$query = $conn->query("SELECT * FROM item WHERE price BETWEEN 11 AND 20");
}
else { $query = $conn->query("SELECT price * FROM item");
}
?>
Whenever I hit a checkbox mark it doesn't do anything it just shows a test Item I am pulling individually. The code below is just a test to make sure its pulling data from the database. Right now I am just pulling price ranges from 0 - 5 and it works. But the above code for the user to have a choice on selecting price ranges it wont do anything. Anything helps.
?php
//get product rows
$query = $conn->query("SELECT * FROM item WHERE price BETWEEN 0 AND 5 ");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
?>
<div class="list-item">
<h2><?php echo $row["name"]; ?></h2>
<h4>Price: <?php echo $row["price"]; ?></h4>
</div>
<?php }
}else{
echo 'Product(s) not found';
} ?>
</div>
</div>
Code is below:
<?php
$conn_error = "Could not connect";
// SQL connection credentials
//They are blanked out since it is connected to the server already
$mysql_host = "";
$mysql_user = "";
$mysql_pass = "";
$mysql_name = "";
$conn = new mysqli($mysql_host, $mysql_user, $mysql_pass,$mysql_name);
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
<?php
//get product rows test to see database is working
$query = $conn->query("SELECT * FROM item WHERE price BETWEEN 0 AND 5 ");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
?>
<div class="list-item">
<h2><?php echo $row["name"]; ?></h2>
<h4>Price: <?php echo $row["price"]; ?></h4>
</div>
<?php }
}else{
echo 'Product(s) not found';
} ?>
</div>
</div>
<body>
<form id="form" method="post" action="">
<input type="checkbox" name="price1" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $0 - 5<br>
<input type="checkbox" name="price2" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $6 - 10<br>
<input type="checkbox" name="price3" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $11 - 20<br>
</form>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('.checkbox').on('change',function(){
$('#form').submit();
});
});
</script>
<?php
if (isset($_POST["price1"])){
$query = $conn->query("SELECT * FROM item WHERE price BETWEEN 0 AND 5");
} elseif (isset($_POST["price2"])){
$query = $conn->queryn("SELECT * FROM item WHERE price BETWEEN 6 AND 10");
} elseif (isset($_POST["price3"])){
$query = $conn->query("SELECT * FROM item WHERE price BETWEEN 11 AND 20");
} else {
$query = $conn->query("SELECT price * FROM item");
}
?>