简体   繁体   English

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

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

what I'm trying to achieve is retrieving data from database with dropdown list. 我想要实现的是从带有下拉列表的数据库中检索数据。

I can get the name variable from database and I can show all the database elements with a table but I don't know how can I merge these two. 我可以从数据库中获取name变量,并且可以用表显示所有数据库元素,但是我不知道如何合并这两个。 I want to click a button and show the selected dropdown list item's all database elements/column data. 我想单击一个按钮,并显示所选下拉列表项的所有数据库元素/列数据。

Here is the code that retrieves product names for dropdown list: 以下是用于检索下拉列表的产品名称的代码:

<?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>

And this is the code that shows all database elements: 这是显示所有数据库元素的代码:

<!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>

I want to click a button and show the selected dropdown list item's all database elements/column data. 我想单击一个按钮,并显示所选下拉列表项的所有数据库元素/列数据。 How can I achieve this? 我该如何实现? Thank you. 谢谢。

You can use jquery & ajax to achieve above .whenever button is click your jquery click function will get executed ,which will get value of select box with class="select" and pass it to your php page , like below : 您可以使用jquery & ajax实现以上。每当按钮被click您的jQuery的click功能将得到执行,这将获得的价值select boxclass="select" ,并把它传递给你的php页面,如下图所示:

Your button : 您的按钮

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

Jquery & Ajax : 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 code : 邮递区号

 <?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();



    ?>

Hope this helps ! 希望这可以帮助 !

暂无
暂无

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

相关问题 使用php和mysql中的下拉选择列表从databse检索数据 - Retrieving data from databse using the dropdown selected list in php and mysql 使用 ajax 和 php 从数据库检索数据到 html 下拉列表时出现问题? - Getting issue while retrieving data from database into html dropdown using ajax and php? 使用PHP和AJAX从MySQL数据库中选择和检索数据 - Selecting and retrieving Data from a MySQL Database using PHP and AJAX 使用php从mysql中选择数据,显示在html下拉列表中,并通过javascript插入所选值作为URL参数 - Select data from mysql with php, display in html dropdown list and insert selected value as URL parameter via javascript 如何使用我的 mysql 数据库中的数据创建下拉列表? - How to create a dropdown list with data from my mysql database? 从mysql和php检索json数据到jquery - retrieving json data from mysql and php into jquery 从Firebase数据库表中检索列表中的数据 - Retrieving data in a list from firebase database table 从下拉列表中选择选项,取决于两个下拉列表,这将显示mysql数据库php的主题选项 - Select option from dropdown list depending on two dropdown list which will show subject options from mysql database php 从使用 PHP 从 MySQL 检索数据的动态列表中的选定选项中获取值 - Get value from a selected option in a dynamic list retrieving data from MySQL with PHP 检索 HTML 动态表单字段数据并保存到 MySQL 数据库 - Retrieving HTML dynamic form fields data and saving to MySQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM