[英]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 box
与class="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.