简体   繁体   English

MySQL和表格:基于第一个选择填充第二个选择选项(来自同一行)

[英]MySQL & Form: Populate second select options based on first selected (From same row)

My problem is that I have three different stock sizes for each product that each contain a different amount of stock (large box, medium box, small box). 我的问题是,每种产品都有三种不同的库存量,每种库存量都有不同的库存量(大包装盒,中包装盒,小包装盒)。 Each product has the same box size but the quantity per box changes. 每种产品都有相同的包装盒大小,但是每包装盒的数量会发生变化。 When the product name is selected I want to then choose the box size and the quantity value should be set corresponding to the product row in the db. 选择产品名称后,我要选择包装盒的大小,并应根据数据库中的产品行设置数量值。

------------------------------------------
|product      |small  |medium   |large   |
------------------------------------------
|Widget one   |100    |275     |400      |
------------------------------------------
|Widget two   |45     |100     |150      |
------------------------------------------
|Widget three |125    |150     |250      |

I already use php to get the select option product names from the db 我已经使用php从数据库获取选择选项产品名称

    $product_select = mysql_query("SELECT * FROM products");

    echo '<select name="Product" id="product">'; 
    while ($row_item = mysql_fetch_array($product_select)){ 
    $pname = $row_item["product"]; 
    echo '<option value="' . htmlspecialchars($pname) . '">' . htmlspecialchars($pname)    . '</option>';

Then I need something like this that gets the row values for small medium and large based on the selected product from above: 然后,我需要类似这样的东西,它根据上面选择的产品获取中小型和大型的行值:

<select><option value=" ">Small</option><option value=" ">Medium</option><option value=" ">Large</option></select>

EDIT 编辑

I have been trying to figure this out all night. 我一直想通宵达旦。 Is this even possible? 这有可能吗? should I just try and go a different route? 我应该试着走另一条路吗?

You have 3 ways to implement this: 您有3种方法可以实现此目的:

  1. Use Optgroup tags inside one select tag. 在一个选择标签内使用Optgroup标签。
  2. Use Ajax to get sizes and puplating second select after choosing product in the first select. 在第一选择中选择产品后,使用Ajax获取尺寸并拼写第二选择。
  3. Store serialized values of stock inside first select and populate second select from that values. 将股票的序列化值存储在第一选择中,然后从该值中填充第二选择。 For example: 例如:

      <select> <option values="100|275|400">Widget one</option> ... </select> 

暂无
暂无

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

相关问题 如何使用PHP根据第一个选择标签中选择的选项填充第二个选择标签? - How do I use PHP to populate a second select-tag with options based on the option selected in the first select-tag? MySQL CodeIgniter是否基于第二个表的第一个Select列进行选择 - MySQL CodeIgniter Select or not from second table based on column of first Select 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table 根据其他DropDownList中的选定值填充第二个DropDownList - Populate a second DropDownList based on a selected value from other DropDownList 隐藏选项根据在第一个选项上选择的选项值选择值 - hide option select value based on option value selected on first options 如何使用jquery从多维json数组中提取值以基于第一个下拉列表的选定值填充第二个下拉列表? - How to use jquery to pull values from a multidimensional json array to populate a second dropdown based on the selected value of first drop down? PHP MySQL的:从数据库中选择并填写表格 - php mysql: select from database and populate form jQuery中的第一个选择框填充第二个选择框 - Second select box populate from first selectbox in jquery MySQL从1查询中选择第二行 - Mysql Select Second Row from 1 query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM