如何从下拉列表中检索数据到另一个下拉列表? [英] How to retrieve data from dropdown list to another dropdown list?

查看:88
本文介绍了如何从下拉列表中检索数据到另一个下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何解释.它让我感到困惑,因为我仍然是这个php世界中的新手.希望你们能帮助我.

我有一个表格.在该表格中有两个下拉列表.第一个下拉列表将显示已保存在数据库中的所有位置.问题在于,如何根据在第一个下拉列表中选择的位置在第二个下拉列表中显示ID号.

有人可以帮助我吗?

代码:

  <form action="form.php" method="post" name="order">
<table>
 <tr>
    <td>Select Location :</td>
    <td> <select name="location" id="location">
    <option>-- Location --</option>
    <?php


$query="SELECT DISTINCT  location FROM inventory";
        $result=mysql_query($query);
            while(list($locationid)=mysql_fetch_row($result)) {
            echo "<option value=\"".$location."\">".$location."</option>";
    }
   ?>
    </select>

      </td>
    </tr>

    <tr>
    <td>Date :</td>
    <td><input type="text" nama="date" /></td>
     </tr>

  <td>Id Number:</td>

    <?php
   $query="SELECT DISTINCT  idno FROM asset WHERE locationid='inventory.location'";
    $result=mysql_query($query);
    while(list($idno)=mysql_fetch_row($result)) {
        echo "<option value=\"".$idno."\">".$idno."</option>";
    }
   ?>

       </td>
</tr>
<tr><td>
<input type="button" value="submit" name="submit" /> </td></tr>
</table>

解决方案

您有3个主要选项:

  1. 一次调用PHP页面即可一次完成所有操作,以呈现该页面的HTML,而不会回发到另一页面并在浏览器中刷新,也不会在第二个下拉菜单中使用JSON的任何AJAX调用.取而代之的是,您将两个"下拉菜单的所有可能值推送"到浏览器,并使用Javascript根据您在第一个下拉菜单中选择的内容来实际缩小第二个下拉列表中的显示值列表.

  2. 使用2次调用"操作到php页面,第一次用第一个下拉菜单中的所有值呈现第二个页面中的所有值,然后第一次显示该页面,那么当您从第一个下拉列表中选择值时,您的page/form/dropdown会触发您的PHP页面的回发(您的第二次调用),这将重新填充第二个下拉菜单,但仅提供适当的(缩小的)选项.

  3. 使用2个电话执行此操作,但不重新渲染页面且不回发.相反,服务器上将有2个PHP页面/脚本.

    1. 第一个脚本使用2个下拉菜单呈现HTML页面,并用其值填充第一个.
    2. 在第一个下拉列表中选择一个选项后,将触发对第二个脚本的AJAX请求.当在PHP服务器上调用该页面/脚本时,它不呈现和发送HTML,而是仅代表表示第二个下拉列表的适当(缩小的)值的JSON.在浏览器中启动的AJAX调用是通过Javascript完成的,它等待JSON返回.完成后,您将对其进行处理并在第二个下拉菜单中更改值".

    这可以通过本机Javascript或jQuery完成.但是,使用jQuery变得容易得多.

I'm not sure how to explain . Its confusing to me as I'm still a newbie in this php world. Hope you guys can help me.

I have a form. In that form there are two dropdown list. The first dropdown list will display all the location that have been save in database. The problem is that how can I, display the id number in the second dropdown list based on the location that have been selected in the first dropdown list .

Can anyone help me ?

Code:

  <form action="form.php" method="post" name="order">
<table>
 <tr>
    <td>Select Location :</td>
    <td> <select name="location" id="location">
    <option>-- Location --</option>
    <?php


$query="SELECT DISTINCT  location FROM inventory";
        $result=mysql_query($query);
            while(list($locationid)=mysql_fetch_row($result)) {
            echo "<option value=\"".$location."\">".$location."</option>";
    }
   ?>
    </select>

      </td>
    </tr>

    <tr>
    <td>Date :</td>
    <td><input type="text" nama="date" /></td>
     </tr>

  <td>Id Number:</td>

    <?php
   $query="SELECT DISTINCT  idno FROM asset WHERE locationid='inventory.location'";
    $result=mysql_query($query);
    while(list($idno)=mysql_fetch_row($result)) {
        echo "<option value=\"".$idno."\">".$idno."</option>";
    }
   ?>

       </td>
</tr>
<tr><td>
<input type="button" value="submit" name="submit" /> </td></tr>
</table>

解决方案

You have 3 main options:

  1. Do it "all at once" with one call to your PHP page to render the HTML for the page and NO postbacks to another page and refresh in the browser, NOR any AJAX call for JSON for the 2nd dropdown. Instead you would "push" all possible values for BOTH dropdowns to the browser and use Javascript to actually narrow the list of displayed values in the 2nd dropdown depending on what you selected in the first

  2. Do it with 2 "calls" to your php page, the first time rendering the page with all values in the 1st dropdown and NO values in the 2nd, then when you selected the value from the 1st dropdown, your page/form/dropdown would trigger a postback (your 2nd call) to your PHP page, which would RE-RENDER the page with the 2nd dropdown ALSO filled, but with only the appropriate (narrowed-down) options.

  3. Do it with 2 calls, but without RE-RENDERING the page and NO POSTBACK. Instead, you would have 2 PHP pages/scripts on the server.

    1. The first script renders the HTML page with the 2 dropdowns and fills the first one with its values.
    2. Upon selection of a choice in the first dropdown, an AJAX request to your 2nd script is trigger. When that page/script is called on the PHP server, it DOES NOT render and send HTML, but instead, only JSON that represents the appropriate (narrowed-down) values for the 2nd drop-down. The AJAX call initiated in the browser is done via Javascript that waits for the JSON to return. When it does, you then process it and "change the values" in the 2nd dropdown.

    This can be done with Native Javascript or jQuery. But, it's much, much easier with jQuery.

这篇关于如何从下拉列表中检索数据到另一个下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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