![](/img/trans.png)
[英]Jquery autocomplete and PHP: populating input field with data from mySQL database based on selected option in autocomplete field
[英]Get variable from database table column into input field based selected option from option field
我已经成功地从我的表中检索了两列,并在我的表单中将结果显示为选择标签中的选项。
问题是我无法根据所选选项将第三列中的数据显示到输入字段中,该字段也在表单中。
数据库连接或检索 select 标记中选项的数据没有问题。
任何帮助,将不胜感激。 相关代码如下。
<div class="col-md-4 col-12 bottommargin-sm">
<label for="">Choose currency</label>
<select class="form-control" id="exampleFormControlSelect1">
<option value="">Choose currency...</option>
<?php
// Including DB connection file
include_once "config/config.php";
// Retrieving all columns from currency table
$result = mysqli_query($con, "SELECT * FROM currency_test");
// Loop printing options with country and currrency for all rows in currency table
while($row = mysqli_fetch_array($result)) {
print "<option>";
echo $row['country'] . " - " . $row['currency'];
print "</option>";
}
// Closing DB connection
mysqli_close($con);
?>
</select>
</div>
<div class="col-md-2 col-12 bottommargin-sm">
<label for="">Currency rate</label>
<input type="text" class="form-control" value="<?php echo $row['buy_rate'] ?>" readonly>
</div>
附加编辑
我现在正在使用以下代码,但仍然没有任何运气:
<div class="col-md-4 col-12 bottommargin-sm">
<label for="">Choose currency/label>
<select class="form-control" id="exampleFormControlSelect1">
<option value="default">Choose currency...</option>
<?php
// Including DB connection file
include_once "config/config.php";
// Retrieving all columns from currency table
$result = mysqli_query($con, "SELECT * FROM currency_test");
// Loop printing country, currrency, buy_rate and sell_rate for all rows in currency table
while($row = mysqli_fetch_array($result)) {
echo "<option value=".$row['currency']."> ".$row['country']. " - " .$row['currency']." </option>";
}
?>
</select>
</div>
<div class="col-md-2 col-12 bottommargin-sm">
<label for="">Currency rate</label>
<input type="text" class="form-control" value="<?php echo $row['buy_rate'] ?>" readonly>
</div>
<?php
// Closing DB connection
mysqli_close($con);
?>
如前所述,您在上面的 while 循环中有一个结果 SET,但您希望输入字段只包含一个值:所选货币的购买率。 如果我猜对了,这就是你想要的,这不会像这样工作。
如果您想测试它并且只在您的 while 循环中检索单个值,那么您的输入字段也会填充正确的购买率。
您可以使用或不使用 JavaScript 来实现,但是您需要第二个语句来检索您想要的购买率。
进一步的安全建议:您不应该使用没有 __ DIR __ 的包含指令。 你也不应该使用“SELECT * ...”。 最好是命名要检索的所有字段。
您必须有该选择的声明。 (提示:同样在标签中,您必须设置一个 value 属性)如果您使用提交按钮,您的选择也需要一个名称。
现在,当您提交表单时,您可以查询数据库并显示正确的记录。
如果您不想使用提交按钮并通过选择更改文本字段中的值,您将需要 javascript。
所以我看到的是:
1) 选择标签中没有名称
<select class="form-control" id="exampleFormControlSelect1">
没有名称属性。 所以在发布表单后,服务器不知道变量的名称。
2) 没有价值
print "<option>";
您不提供值。 因此,您的服务器在发布表单后不知道该值(用户选择的)。
这是选择标签的正确方法: https : //www.w3schools.com/tags/tag_select.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.