douyong1850 2014-11-07 07:28
浏览 22
已采纳

仅在选择了值时才显示下拉菜单中的结果

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 i.e 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

P.S (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>
  • 写回答

1条回答 默认 最新

  • dpkt31779 2014-11-07 07:41
    关注

    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); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog