![](/img/trans.png)
[英]Fetch data from api(RESTful) db(mongodb) according to user input
[英]fetch data from db depending on what user select
我是后端和 PHP 的新手。 我試圖根據用戶從下拉菜單中選擇的內容從我的數據庫中獲取數據到輸入字段。 我在下拉菜單中有從數據庫中獲取的值。 如果用戶單擊下拉列表中的元素,則其來自數據庫的數據應顯示在輸入字段中。 另一個問題,我應該為此編寫一個新的 php 頁面嗎? 或者這會發生在同一個 html 頁面上嗎? 一個小代碼示例會很有幫助,謝謝:)
<div class="sidenav">
<button class="dropdown-btn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-container">
<a href="#">value 1 from db </a>
<a href="#">value 2 from db</a>
<a href="#">value 3 from db</a>
</div>
</div>
<form name="f" action="" method="">
<div class="main">
<table>
<tr>
<th colspan="2"></th>
</tr>
<tr>
<td style="border-bottom: 0;"> Name </td>
<td style="border-bottom: 0;"> <input type="text" name="Name" value=""> </td>
</tr>
<tr>
<td style="border-bottom: 0;"> Adress </td>
<td style="border-bottom: 0;"> <input type="text" name="Adress" value=""> </td>
</tr>
<tr>
<td style="border-bottom: 0;"> ZIP </td>
<td style="border-bottom: 0;"> <input type="text" name="ZIP" value=""> </td>
</tr>
</table>
</div>
</form>
樣式表代碼
/* Fixed sidenav, full height */
.sidenav {
height: 100%;
width: 200px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 20px;
}
/* Style the sidenav links and the dropdown button */
.sidenav a, .dropdown-btn {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 20px;
color: #818181;
display: block;
border: none;
background: none;
width: 100%;
text-align: left;
cursor: pointer;
outline: none;
}
/* Main content */
.main {
margin-left: 200px; /* Same as the width of the sidenav */
font-size: 20px; /* Increased text to enable scrolling */
padding: 0px 10px;
}
/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
display: none;
background-color: #262626;
padding-left: 8px;
}
和一個用於下拉菜單的小 javascript 代碼
/* Loop through all dropdown buttons to toggle between hiding and showing its dropdown content - This allows the user to have multiple dropdowns without any conflict */
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function() {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
這是工作代碼的現場演示
https://jsfiddle.net/m4sxnzg0/1/
非常感謝!
如果您想使用 Ajax(我建議),您可以通過這篇文章了解如何使用: 通過 jQuery/Ajax 調用 PHP 頁面
如果您只想使用 PHP,您可以將頁面和變量重新加載到 URL,並在 PHP 中使用 $_GET 檢索它們。
在此處查找更多信息: https : //www.php.net/manual/en/reserved.variables.get.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.