根据选定的下拉值填充表

时间:2017-02-21 10:33:37

标签: php html-table dropdown

我想知道在从下拉列表中选择值后,条件在表中显示数据的位置。

两者都有相同的ID(下拉列表和表格)。

  

php table

<html>    
<head>
</head>
<body>
    <?php
    $con=mysqli_connect("localhost","root","root","company");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql="SELECT employees.id,employees.jobs FROM employees WHERE employees.jobs in ("programmer","hr","qa")";
    if ($result=mysqli_query($con,$sql)){
        ?>
        <label for="y">Select the job:</label>
        <select name="loads" id="loads" onchange=""> 
        <?php while($ri = mysqli_fetch_array($result)) {
            ?>
            <option value="<?php echo $ri['id'];?>" >  <?php echo $ri['jobs']; ?> </option>
            <?php
        }
    }
    ?>
    </select> 
    <table class="striped" border="1" align="center" id="demo">
        <tr class="header">
            <td align="center"><b>Name</b></td>
        </tr>
        <?php
        $con=mysqli_connect("localhost","root","root","company");
        // Check connection
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $sql2="SELECT employees.id,employees.name FROM employees WHERE employees.jobs in ("programmer","hr","qa")";

        if ($result=mysqli_query($con,$sql2)){
            // Fetch one and one row
            while ($row=mysqli_fetch_array($result)){
                echo "<tr>";
                echo "<td>" . $row["name"] . " " . "</td>";
                echo "</tr>";
            }
        }

        mysqli_close($con);
        ?>
    </table>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果您的意思是:当您在下拉列表中选择一个项目时(选择标记),您希望表格发生变化。然后它是不可能通过PHP,因为PHP代码在页面上的每次加载后编译一次,它不能正常工作! 所以你必须使用JQUERY和AJAX来做到这一点。

如果这是您要搜索的内容,请回复我,以便我可以帮助您。

顺便说一下,你不需要连接2次数据库并运行相同的查询,我只是编辑了你的代码:

    <html>
<head>
    <title></title>
</head>
<body>
    <?php $con = mysqli_connect("localhost","root","root","company");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql="SELECT * FROM employees WHERE employees.jobs in ("programmer","hr","qa")";
    $result = mysqli_query($con, $sql);
    if ($result) { ?>

        <label for="y">Select the job:</label>
        <select name="loads" id="loads" onchange="">
        <?php while($ri = mysqli_fetch_array($result)) { ?>
            <option value="<?php echo $ri['id'];?>" >  <?php echo $ri['jobs']; ?> </option>
            <?php
        }
    }
    ?>
    </select>
    <table class="striped" border="1" align="center" id="demo">
    <tr class="header">
        <td align="center"><b>Name</b></td>
    </tr>
    <?php
    // Fetch one and one row
    while ($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row["name"] . " " . "</td>";
        echo "</tr>";
    }

    mysqli_close($con);
    ?>
</table>

</body>
</html>