微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决根据用户选择从数据库中获取数据

是Backend和PHP的新手。我试图根据用户从下拉菜单中选择的内容将数据从数据库提取到“输入”字段。我从数据库获取下拉菜单中的值。如果用户单击下拉列表中的元素,则来自DB的元素数据应显示在输入字段中。 另一个问题,我应该为此编写一个新的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(我建议使用),可以通过这篇文章找出方法:Call PHP page via jQuery/Ajax

如果您只想使用PHP,则可以将页面和变量重新加载到URL,然后使用$ _GET在PHP中检索它们。

在此处查找更多信息:https://www.php.net/manual/en/reserved.variables.get.php

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?