动态下拉框从MySQL [英] dynamic drop down box from mysql

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

问题描述

我有3个输入字段,每个字段都分别从它自己的表中获取名为Tour类型,国家和目的地的数据,如图所示

 < label> Tour Type< / label> 
< select id =element_11name =element_11required>
< option value =selected =selected> - 选择 - < / option>
<?php
while($ row = mysql_fetch_array($ sql))
{
$ tour_type_id = $ row ['tour_type_id'];
$ name = $ row ['tour_name'];
echo< option value ='$ tour_type_id'> $ name< / option>;
}
?>
< / select>



< label>国家< / label>
< select id =element_12name =element_12required>
< option value =selected =selected> - 选择 - < / option>
<?php
$ sql = mysql_query(Select countries_id,countries_name from countries);
while($ row = mysql_fetch_array($ sql))
{
$ cid = $ row ['countries_id'];
$ name = $ row ['countries_name'];

echo< option value ='$ cid'>。$ name。< / option>;
}
?>
< / select>


< label>目的地< / label>
< select id =element_13name =element_13required>
< option value =selected =selected> - 选择 - < / option>
<?php
$ sql = mysql_query(Select destination_id,destination_name from destination);


while($ row = mysql_fetch_array($ sql))
{
$ destination_id = $ row ['destination_id'];
$ name = $ row ['destination_name'];

echo< option value ='$ destination_id'>。$ name。< / option>;
}
?>

< / select>
< / div>
< / li>

这就是我的3个数据库表,分别是tourtype,countries和destination。








我正在尝试使每个领域相互依赖更像一个依赖的下拉框。例如,如果我选择一个游览类型,那么第二个下拉菜单应该填充仅与从第一个下拉菜单中选择的选项相关的选项,依此类推。在这种情况下,例如,如果我选择文化,那么第二次下拉应该只显示阿姆斯特丹和比利时。



任何人都可以帮助我解决这个问题。

$ b

解决方案

请通过此链接使用jquery ajax的依赖下拉菜单



他如何维护实体之间的关系



让我解释一下,如果您希望 countries 根据旅程,那么您需要将国家表巡视表,因为您已经在图片中显示国家/地区,它只包含两列 countries_id countries_name 您必须在添加一列 tour_type_id 时选择任何你会得到 tour_type_id 的游览,那么你的查询应该是

  SELECT * FROM`countries`,其中`tour_type_id` = 1 // th是从tour_type选择框中获得的id 

,这会填充相关国家的相同大小写对于与 country_id



的此目的地相关的目的地而言,希望它有意义


I got 3 input fields and each field are getting their data from its own tables called Tour type, country and destination respectively as shown

    <label>Tour Type</label>
    <select id="element_11" name="element_11" required> 
    <option value="" selected="selected">--Select--</option>
    <?php
        while($row=mysql_fetch_array($sql))
        {
          $tour_type_id=$row['tour_type_id'];
          $name=$row['tour_name'];
          echo "<option value='$tour_type_id'>$name</option>";
        }
        ?>
    </select>



    <label>Country</label>
    <select id="element_12" name="element_12" required>
    <option value="" selected="selected">-- Select --</option> 
    <?php 
    $sql=mysql_query("Select countries_id,countries_name from countries");
        while($row=mysql_fetch_array($sql))
        {
          $cid=$row['countries_id'];
          $name=$row['countries_name'];

          echo "<option value='$cid'>".$name."</option>";
        }
        ?>
        </select>


    <label>Destination</label>
    <select id="element_13" name="element_13" required> 
        <option value="" selected="selected">-- Select --</option>
    <?php 
    $sql=mysql_query("Select destination_id,destination_name from destination");


        while($row=mysql_fetch_array($sql))
        {
          $destination_id=$row['destination_id'];
          $name=$row['destination_name'];

          echo "<option value='$destination_id'>".$name."</option>";
        }
        ?>

    </select>
    </div> 
    </li>

This is what i got as my 3 database tables i.e. tourtype, countries and destination respectively:

I am trying to make each field dependent on each other more like a dependent drop down box. For example if i select a tour type then the 2nd drop down should populate options only relevant to what is selected from the 1st drop down and so on. In this case for e.g if i select culture ,then the 2nd drop down should only show amsterdam and belgium.

Can anyone help me on this.

解决方案

Please go through this link dependent dropdown using jquery ajax

How he maintained the relations among the entities

let me explain you if you want countries based on tour then you need to relate the country table with tour table as you have shown the country table in image it contains only two columns countries_id and countries_name you have to add one more column that is tour_type_id when you select any tour you will get the tour_type_id then your query should be

SELECT * FROM `countries`  where `tour_type_id` = 1 //this is the id you will get from the tour_type select box

and this will populate the related countries same case for the destination related this table with country_id

Hope it makes sense

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

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