繁体   English   中英

使用下拉列表PHP / MySQL / HTML从数据库中检索数据

[英]Retrieving data from database with dropdown list PHP/MySQL/HTML

我想要实现的是从带有下拉列表的数据库中检索数据。

我可以从数据库中获取name变量,并且可以用表显示所有数据库元素,但是我不知道如何合并这两个。 我想单击一个按钮,并显示所选下拉列表项的所有数据库元素/列数据。

以下是用于检索下拉列表的产品名称的代码:

<?php

$mysqli = NEW MySQLi('localhost', 'root', '', 'mydb');
$resultSet = $mysqli->query("SELECT namepro FROM mytable");

?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>developer mode</title>
</head>
<body>

<select>
<?php
while($rows = $resultSet->fetch_assoc()){
  $dropdown = $rows['namepro'];
  echo "<option value='$dropdown'>$dropdown</option>";
}
?>
</select>
<button type="button">Click Me!</button>

</body>
</html>

这是显示所有数据库元素的代码:

<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<table>
<tr>
    <th>name</th>
    <th>size</th>
</tr>
<?php

$conn=mysqli_connect("localhost","root","","mydb");
$sql = "SELECT namepro, res FROM mytable";
$result = $conn -> query($sql);
if($result->num_rows > 0){
    while($row=$result->fetch_assoc()){
        echo "<tr><td>". $row["namepro"]. "</td><td>". $row["res"]. "</td></tr>";
    }
    echo "</table>";
}
else{
    echo "0 result";
}

$conn ->close();



?>
</table>



</body>
</html>

我想单击一个按钮,并显示所选下拉列表项的所有数据库元素/列数据。 我该如何实现? 谢谢。

您可以使用jquery & ajax实现以上。每当按钮被click您的jQuery的click功能将得到执行,这将获得的价值select boxclass="select" ,并把它传递给你的php页面,如下图所示:

您的按钮

<button type="button" class="btn">Click Me!</button>
 <div id="show"></div> // here data from respone will be display

jQuery和Ajax

<script>
$(document).ready(function () {
     $(".btn").click(function () {
            var value= $(".select").val();//getting value of select box with class="select"
            $.ajax({
                url: 'yourphppage',
                method: 'POST',
                data: {value : value},//sending value to yourphppage
                     success:function(data){

                $("#show").html(data);//here response from server will be display
                }
              });
            });
         });
</script>

邮递区号

 <?php
    $value=$_POST['value'];//getting value sent from ajax
    $conn=mysqli_connect("localhost","root","","mydb");
    $sql = "SELECT namepro, res FROM mytable where namepro='".$value."'";//passing in query
    $result = $conn -> query($sql);

    if($result->num_rows > 0){
         echo "<table>
      <tr>
    <th>name</th>
    <th>size</th>
      </tr>";
        while($row=$result->fetch_assoc()){
            echo "<tr><td>". $row["namepro"]. "</td><td>". $row["res"]. "</td></tr>";
        }
        echo "</table>";
    }
    else{
        echo "0 result";
    }

    $conn ->close();



    ?>

希望这可以帮助 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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