简体   繁体   中英

display result from dropdown menu only when value from it is selected

This code is a demo part of my search form. the actual search form contains 3 dropdown list, values selected from the dropdown list acts as a keyword on basis of which search is conducted. This dropdown list contains fruits such as mango, apple, grapes etc. search code is working fine. But the issue is that the first option displayed in the dropdown list is Select (that holds no value), below which the actual list of fruit starts, but still the value of first fruit is getting selected when the page is getting loaded for the first time . What i am trying to do is that when the page loads for the first time ie the value in the drop down list is Select nothing should get displayed and after that when the user selects the value from dropdown and hits the submit button then only then result should get displayed

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) {
                    die("Connection failed: " . mysqli_connect_error());
                }

                $sql = "SELECT fruits FROM fruits";
                $result = $con->query($sql);
                echo "<label for='fruits'>Treatment Type: </label>";
                echo "<select name='fruits' id='fruits' class='form-control'><option value=''>--Select--</option>";
                while($row = $result->fetch_assoc()) {
                echo "<option value='" . $row['fruits'] . "'>" . $row['fruits'] . "</option>";
                }
                echo "</select>";
            ?>
        </div>
    </div>
</div>

Code that does the search part

<?php
    $con=mysqli_connect("","","","");// Check connection
    if (mysqli_connect_errno()) 
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $fruits = mysqli_real_escape_string($con, $_POST['fruits']);

        $sql1 = "SELECT * FROM treatment WHERE fruits LIKE '%$fruits%'";
        $result = mysqli_query($con, $sql1);
        echo "<table class='table table-striped table-bordered responsive'>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Fruits</th>
                </tr>
            </thead>";

            if (mysqli_num_rows($result) > 0) 
                {
                    while($row = mysqli_fetch_assoc($result)) 
                        {
                            echo "<tbody data-link='row' class='rowlink'>";
                                echo "<tr>";
                                    echo "<td><a href='#'>" . $row['name'] . "</a></td>";
                                    echo "<td>" . $row['type'] . "</td>";       
                                    echo "<td>" . $row['fruits'] . "</td>";

                                echo "</tr>";
                            echo "</tbody>";    
                        }

                }
            else 
                {
                    echo "0 results";
                }
            echo "</table>";
            mysqli_close($con);
?>

Would appreciate if someone can guide me

PS (edited part)

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) 
                {
                    die("Connection failed: " mysqli_connect_error());
                }

                $sql = "SELECT fruits FROM fruits";
                $result = $con->query($sql); ?>
                echo "<label for="fruits">Treatment Type: </label>";
                echo "<select name="fruits" id="fruits" class="form-control">
                <option value="" <?php if(!isset($_POST['fruits'])) { ?>selected<?php } ?>>--Select--</option>";
                <?php 
                while($row = $result->fetch_assoc()) { ?>

                echo "<option value="<?php echo $row['fruits']; ?>" <?php if(isset($_POST['fruits']) && $_POST['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>";
                <?php } ?>
            </select>
        </div>
    </div>
</div>

In the <option> portions, just modify a bit. Add selected with conditions:

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) {
                    die("Connection failed: " . mysqli_connect_error());
                }

                $sql = "SELECT fruits FROM fruits";
                $result = $con->query($sql); ?>
            <label for="fruits">Treatment Type: </label>
            <select name="fruits" id="fruits" class="form-control">
                <option value="" <?php if(!isset($_POST['fruits']) || (isset($_POST['fruits']) && empty($_POST['fruits']))) { ?>selected<?php } ?>>--Select--</option>
                <?php 
                while($row = $result->fetch_assoc()) {
                ?>
                <option value="<?php echo $row['fruits']; ?>" <?php if(isset($_POST['fruits']) && $_POST['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>
                <?php } ?>
            </select>
        </div>
    </div>
</div>

Results page:

<?php
    $con    =   mysqli_connect("","","","");// Check connection
    if(mysqli_connect_errno()) 
        echo "Failed to connect to MySQL: " . mysqli_connect_error();

        $fruits =   mysqli_real_escape_string($con, $_POST['fruits']);

        if(!empty($fruits)) {
            $sql1   =   "SELECT * FROM treatment WHERE fruits LIKE '%$fruits%'";
            $result =   mysqli_query($con, $sql1);
            $count  =   mysqli_num_rows($result);
        }
        else
            $count  =   0; ?>

        <table class='table table-striped table-bordered responsive'>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Fruits</th>
                </tr>
            </thead>
        <?php
            if ($count > 0)  {
                while($row = mysqli_fetch_assoc($result)) {
        ?>
            <tbody data-link='row' class='rowlink'>
                <tr>
                    <td><a href='#'><?php echo $row['name']; ?></a></td>
                    <td><?php echo $row['type']; ?></td>
                    <td><?php echo $row['fruits']; ?></td>
                </tr>
            </tbody>
        <?php
                }
            }
            else
                echo "0 results";
        ?>
        </table>
        <?php mysqli_close($con); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM