当select的值为ID时,从mysql获取数据到输入标记

时间:2022-11-19 19:10:24

I have MySQL table "names":

我有MySQL表“名称”:

  id |    name   |   country  |    age  
  1  |    Mark   |   Germany  |     20  
  2  |    Jack   |    UK      |     23  
  3  |    Mary   |    US      |     32  
  4  |   Suzan   |   Italy    |     15  

What I need is :

我需要的是:

  • When I select 2, I want set value of input tag to 23
  • 当我选择2时,我希望输入标签的设定值为23

  • When I select 4, I want set value of input tag to 15
  • 当我选择4时,我希望输入标签的设定值为15

<html>
    <body>
        <select  id="name" name="name"  required  >
            <option></option>
            <option  value="<?php echo $row['id'];?>" >
                <?php echo $row['name']; ?> - <? echo $row['country']; ?> - <? echo $row['age']; ?>  
            </option> 
        </select>
        <input type="number"  id="age" name="age" value=" " placeholder=" age" required>
    </body>
</html>

1 个解决方案

#1


0  

There are 2 things you have to do:

你需要做两件事:

First you put the value you want to use as value in the value from the option.

首先,将要用作值的值放在选项的值中。

To simplify the example see HTML the value of the option is the value you need! So no reason to set there an ID and get the age.. even if that is possible there is not reason here to do that..

要简化示例,请参阅HTML,该选项的值是您需要的值!所以没有理由设置一个ID并获得年龄..即使有可能,这里也没有理由这样做..

var mySelect = document.getElementById("name");
mySelect.addEventListener("change", myFunction);

function myFunction()  {
	document.getElementById("age").value = mySelect.options[mySelect.selectedIndex].value;
};
<select  id="name" name="name"  required  >
    <option  value="1" >
        name1 - country1 - age 1
    </option>
    <option  value="2" >
        name2 - country2 - age 2
    </option>
    <option  value="3" >
        name3 - country3 - age 3
    </option>
    <option  value="4" >
        name4 - country4 - age 4
    </option>
</select>
<input type="number"  id="age" name="age" value="" placeholder=" age" required>

Then, seccond: To get that HTML like above you might need to write something like this in PHP

然后,秒:要获得上面的HTML,你可能需要在PHP中编写类似的东西

<select  id="name" name="name"  required  >

    <?php
        // assuming you get your MySQL results in a array called $rows
        foreach($rows as $row) {
            echo '<option  value="'.$row['age'].'">'.
                     $row['name'] . ' - ' . $row['country'] . ' - ' . $row['age']
            .'</option>';
        }
    ?>

</select>
<input type="number"  id="age" name="age" value="" placeholder=" age" required>

#1


0  

There are 2 things you have to do:

你需要做两件事:

First you put the value you want to use as value in the value from the option.

首先,将要用作值的值放在选项的值中。

To simplify the example see HTML the value of the option is the value you need! So no reason to set there an ID and get the age.. even if that is possible there is not reason here to do that..

要简化示例,请参阅HTML,该选项的值是您需要的值!所以没有理由设置一个ID并获得年龄..即使有可能,这里也没有理由这样做..

var mySelect = document.getElementById("name");
mySelect.addEventListener("change", myFunction);

function myFunction()  {
	document.getElementById("age").value = mySelect.options[mySelect.selectedIndex].value;
};
<select  id="name" name="name"  required  >
    <option  value="1" >
        name1 - country1 - age 1
    </option>
    <option  value="2" >
        name2 - country2 - age 2
    </option>
    <option  value="3" >
        name3 - country3 - age 3
    </option>
    <option  value="4" >
        name4 - country4 - age 4
    </option>
</select>
<input type="number"  id="age" name="age" value="" placeholder=" age" required>

Then, seccond: To get that HTML like above you might need to write something like this in PHP

然后,秒:要获得上面的HTML,你可能需要在PHP中编写类似的东西

<select  id="name" name="name"  required  >

    <?php
        // assuming you get your MySQL results in a array called $rows
        foreach($rows as $row) {
            echo '<option  value="'.$row['age'].'">'.
                     $row['name'] . ' - ' . $row['country'] . ' - ' . $row['age']
            .'</option>';
        }
    ?>

</select>
<input type="number"  id="age" name="age" value="" placeholder=" age" required>