简体   繁体   English

将变量从数据库表列获取到基于选项字段中选定选项的输入字段

[英]Get variable from database table column into input field based selected option from option field

I have succesfully retrieved two columns from my table and displayed the result as options in a select tag, in my form.我已经成功地从我的表中检索了两列,并在我的表单中将结果显示为选择标签中的选项。

The problem is I cannot display the data from a third column into an input field, which is also in the form, based on the selected option.问题是我无法根据所选选项将第三列中的数据显示到输入字段中,该字段也在表单中。

There is no issue with the database connection or retrieving the data for the options in the select tag.数据库连接或检索 select 标记中选项的数据没有问题。

Any help would be appreciated.任何帮助,将不胜感激。 The relevant code is below.相关代码如下。

     <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>

Additional Edit附加编辑

I am now using the following code, still without any luck:我现在正在使用以下代码,但仍然没有任何运气:

     <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);

    ?>

As said before, you have a result SET in your while-loop above, but you want the input field only to contain a single value: the buy-rate of the selected currency.如前所述,您在上面的 while 循环中有一个结果 SET,但您希望输入字段只包含一个值:所选货币的购买率。 If I guessed right and this is what you want, this will not work like this.如果我猜对了,这就是你想要的,这不会像这样工作。

If you want to test it and just retrieve only a single value in your while-loop, also your input field gets filled with the correct buy-rate.如果您想测试它并且只在您的 while 循环中检索单个值,那么您的输入字段也会填充正确的购买率。

You can do it with or without JavaScript, but you need a second statement to retrieve the buyrate you desire.您可以使用或不使用 JavaScript 来实现,但是您需要第二个语句来检索您想要的购买率。

Further security suggestions: You should not use the include directives without __ DIR __ .进一步的安全建议:您不应该使用没有 __ DIR __ 的包含指令。 Neither you should use "SELECT * ...".你也不应该使用“SELECT * ...”。 Better it is to name all the fields you want to retrieve.最好是命名要检索的所有字段。

You will have to have a statement for that selection.您必须有该选择的声明。 (Hint: Also in the tag you will have to have a value attribute set) Also your select needs a name if you use a submit button.. (提示:同样在标签中,您必须设置一个 value 属性)如果您使用提交按钮,您的选择也需要一个名称。

Now when you submit the form you can query the database and show the correct record.现在,当您提交表单时,您可以查询数据库并显示正确的记录。

If you dont want to use a submit button and have the value in the textfield change by selection, you will need javascript.如果您不想使用提交按钮并通过选择更改文本字段中的值,您将需要 javascript。

So what I see :所以我看到的是:

1) No name in select tag 1) 选择标签中没有名称

<select class="form-control" id="exampleFormControlSelect1">

There is no name attribute.没有名称属性。 So after posting the form, the server doesn't know the name of the variable.所以在发布表单后,服务器不知道变量的名称。

2) No value 2) 没有价值

print "<option>";

You don't supply a value.您不提供值。 So your server doesn't know the value (the user selected) after posting the form.因此,您的服务器在发布表单后不知道该值(用户选择的)。

This is the proper way for a select tag : https://www.w3schools.com/tags/tag_select.asp这是选择标签的正确方法: https : //www.w3schools.com/tags/tag_select.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery自动完成和PHP:根据自动完成字段中的选定选项,使用来自mySQL数据库的数据填充输入字段 - Jquery autocomplete and PHP: populating input field with data from mySQL database based on selected option in autocomplete field JavaScript根据选定的选项值将值分配给输入字段 - JavaScript assign value to input field, from selected option value 尝试基于选择选项从数据库自动填充输入字段时出现错误 - Getting an error when trying to autofill input field from database based on a select option 如何从数据库中选择显示值的选项2输入字段 - how to select option 2 input field for show value from database 根据选择的选项添加或删除输入字段的必需属性 - Add or remove the required attribute of an input field based on which option is selected 使用php从HTML获取选择选项值以禁用输入字段 - Get select option value from HTML with php to disable input field 将所选选项的属性添加到变量中以使用SQL显示输入字段 - Attribute selected option into a variable to show input field using SQL 用jQuery用选定的选项填充输入字段 - Fill Input Field with selected option with jquery 根据mysql字段设置所选选项 - Set selected option based on mysql field 从选择选项字段中获取ID并在所选用户中对其进行更新 - Grab id from a select option field and update it in the selected user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM