繁体   English   中英

根据用户选择从数据库中获取数据

[英]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.

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