![](/img/trans.png)
[英]Retrieving data from databse using the dropdown selected list in php and mysql
[英]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 box
與class="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.