I am trying to build php dynamic dropdowns for searching purpose. For example - search by min price and max price. Although it shows the right results, the dropdowns do not hold the POST values. Here is my code
<div class="form-group">
<select class="form-control" name="minprice" id="minprice">
<option value="">--Min Price--</option>
<?php
$args=array(':type' => 'general');
$sql='SELECT DISTINCT price FROM cars WHERE cartype=:type AND price !=" "';
$stmt=$pdo->prepare($sql);
$stmt->execute($args);
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo '<option value="'.$row['price'].'"'.($row['price']==$_POST['price'] ? ' selected="selected"' : '').'>'.$row['price'].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<select class="form-control" name="maxprice" id="maxprice">
<option value="">--Max Price--</option>
<?php
$args=array(':type' => 'general');
$sql='SELECT DISTINCT price FROM cars WHERE cartype=:type AND price !=" "';
$stmt=$pdo->prepare($sql);
$stmt->execute($args);
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo '<option value="'.$row['price'].'"'.($row['price']==$_POST['price'] ? ' selected="selected"' : '').'>'.$row['price'].'</option>';
}
?>
</select>
</div>
if (isset($_POST['submit']))
{
........
.......
$minprice= $_POST['minprice'];
$maxprice= $_POST['maxprice'];
$stmt=$pdo->query("SELECT * FROM cars WHERE cartype='general'
............
...................
AND price >= '".$minprice."'
AND price <= '".$maxprice."'
");
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
include 'carlist.php';
}
}else{.....}