使用 MySQL DB 的动态多级下拉列表 [英] Dynamic multi-level drop down using MySQL DB

查看:36
本文介绍了使用 MySQL DB 的动态多级下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP 创建一个多级下拉菜单,因为我之前没有 javascript 经验,但我也愿意学习.为了让每个人都容易理解,我以汽车为例.我希望用户选择一个品牌"示例福特,然后第二个下拉列表将填充福特制造的模型",最后第三个下拉列表将填充福特汽车的颜色".我正在使用一个 MySQL 数据库,我想从它动态中提取所有数据,而不是对其中的值进行硬编码.我可以在第一个下拉列表中填充品牌",但是当我选择品牌"时,第二个下拉不会填充新结果.我是 PHP 和 MySQL 的新手,但学习速度非常快.这是我的代码:

I am trying to create a multi level drop down menu hopefully using PHP since I have no prior experience in javascript but I am willing to learn that also. To make this easy for everyone to understand I am using cars as and example. I want the user to select a "Brand" example Ford, then a second drop down will populate with the "Models" Ford has made and finally a third drop down will populate with "Colors" of Ford vehicles. I am using a MySQL database that I want to pull all of the data from dynamically instead of hardcoding the values in. I am able to get the first drop down to populate with the "Brands" but when I select a "Brand" the second drop down does not populate with the new results. I am new to PHP and MySQL but a very quick learner. Here is my code:

INSERT_DROPDOWN.PHP

INSERT_DROPDOWN.PHP

 <?php
$con = mysql_connect("localhost","username","XXXXXXXXXX");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

// Write out BRANDS.
$dquery = "SELECT brand FROM manufacture ORDER BY brand";
// Execute it, or return the error message if there's a problem.
$dresult = mysql_query($dquery) or die(mysql_error());

// Write out MODELS.
$dquery1 = "SELECT 'brand', 'model' FROM models WHERE brand='$dresult' ORDER BY model";
// Execute it, or return the error message if there's a problem.
$dresult1 = mysql_query($dquery1) or die(mysql_error());


// Write out COLORS.
$dquery2 = "SELECT color FROM color ORDER BY color";
// Execute it, or return the error message if there's a problem.
$dresult2 = mysql_query($dquery2) or die(mysql_error());

// if successful insert data into database, displays message "Successful". 
if($dresult){
echo "Successful";
echo "<BR />";
}

else {
echo "ERROR1";
}

// close connection 
mysql_close();
?>

测试.PHP

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <LINK href="CLL.css" rel="stylesheet" type="text/css">
        <title> 
           testing
        </title>
    </head>
    <body>

<?php 
require "insert_dropdown.php";
?>

<p>
  <?php
    //Brand
    $dropdown = "<select name='brand'>";
      while($row = mysql_fetch_assoc($dresult)) 
    {

    $dropdown .= "\r\n<option value='{$row['brand']}'>{$row['brand']}</option>";

    }

    $dropdown .= "\r\n</select>";
    echo $dropdown;

    //Model
        $dropdown1 = "<select name='brand'>";
      while($row1 = mysql_fetch_assoc($dresult1)) 
    {

    $dropdown1 .= "\r\n<option value='{$row1['model']}'>{$row['brand']}</option>";

    }

    $dropdown1 .= "\r\n</select>";
    echo $dropdown1;

    //Color
        $dropdown = "<select name='name'>";
      while($row = mysql_fetch_assoc($dresult2)) 
    {

    $dropdown .= "\r\n<option value='{$row['color']}'>{$row['color']}</option>";

    }

    $dropdown .= "\r\n</select>";
    echo $dropdown;
  ?>
</p>
    </body>
</html>

推荐答案

我使用 AJAX 修复了代码

I fixed the code by using AJAX

这篇关于使用 MySQL DB 的动态多级下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆