Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

mysql - php dynamic dropdown not holding value after submission

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{.....}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are just using the wrong names for the $_POST values

The select is called minprice and maxprice and you are testing against $_POST['price'] which does not exist.

<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['minprice'] ? '  selected="selected"' : '').'>'.$row['price'].'</option>';
    //      The correction is here                                              ^^^^^^^
    ?>
</select>

And of course the same issue with maxprice dropdown

To get errors out of PHP even in a LIVE environment add these 4 lines to the top of any MYSQLI_ based script you want to debug ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);. This will force any MYSQLI_ errors to generate an Exception that you can see on the browser as well as normal PHP errors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...