gpt4 book ai didi

PHP/MySQL 在选择另一个时更改一个下拉菜单

转载 作者:行者123 更新时间:2023-11-29 06:38:50 26 4
gpt4 key购买 nike

我希望第二个下拉列表出现在第一个下拉列表中(链接 2 个选择)。如何做到这一点,如果有人可以指导我或给我一个例子,我将不胜感激!

第一个下拉菜单:

<select name="customer">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY customer ORDER BY customer";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($cust = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$cust["customer"]."'".($cust["customer"]==$_REQUEST["customer"] ? " selected" : "").">".$cust["customer"]."</option>"; } ?>
</select>

第二个下拉列表:

<select name="product">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY product ORDER BY product";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["product"]."'".($row["product"]==$_REQUEST["product"] ? " selected" : "").">".$row["product"]."</option>"; } ?>
</select>

Table Structure

最佳答案

首先下拉:

<select name="customer" onchange="this.form.submit()">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY customer ORDER BY customer";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($cust = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$cust["customer"]."'".($cust["customer"]==$_REQUEST["customer"] ? " selected" : "").">".$cust["customer"]."</option>"; } ?>
</select>

第二个下拉列表:

<?php if(isset($_REQUEST['customer'])){ ?>
<select name="product">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE customer = ".$_REQUEST['customer']." GROUP BY product ORDER BY product";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["product"]."'".($row["product"]==$_REQUEST["product"] ? " selected" : "").">".$row["product"]."</option>"; } ?>
</select>
<?php } ?>

您可以在第二个下拉列表中添加一个 where 条件,查询将是,

$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE customer = ".$_REQUEST['customer']." GROUP BY product ORDER BY product";

组合代码:

<form name="demo" action="#">
<select name="customer" onchange="this.form.submit()">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY customer ORDER BY customer";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($cust = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$cust["customer"]."'".($cust["customer"]==$_REQUEST["customer"] ? " selected" : "").">".$cust["customer"]."</option>"; } ?>
</select>

<?php if(isset($_REQUEST['customer'])){ ?>
<select name="product">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE customer = ".$_REQUEST['customer']." GROUP BY product ORDER BY product";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["product"]."'".($row["product"]==$_REQUEST["product"] ? " selected" : "").">".$row["product"]."</option>"; } ?>
</select>
<?php } ?>
</form>

关于PHP/MySQL 在选择另一个时更改一个下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22829367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com