从下拉列表中选择的值到具有根据第一个值的新值的新下拉列表中


from drop list selected value into new drop list with new value according to the first one

我有一个选择框,如下所示。

          <td align="center">
           <select id="selectW" name="type" onChange="this.form.submit()">
             <option value="select...">select</option>
             <option value="dollars">dollars</option>
             <option value="BP">BP</option>
           </select>

我想要的是生成另一个选择框,该框的选项需要根据用户在#selectW元素中选择的MySQL数据库中提取。

我试过这个:

<select id="selectW" name="type" onChange="this.form.submit()">

但是它提交了它在里面的表单。

您需要了解表单的基本工作。您可以通过以下步骤来实现您的结果。

1-让我们更改onChange事件。

<select id="selectW" name="type" onChange="showSecondDropDown()">

将会发生的情况是,当用户从#selectW列表中选择任何值时,一个名为showSecondDropDown()的java脚本函数将被调用,因此是时候添加这个函数JS了。

2-定义JS函数。

<script type="text/javascript">
    function showSecondDropDown() {
       //Now get the value of selectW.
       var userSelected = jQuery("#selectW").val();
       //Now we need to get values from MYSQL based on the "userSelected"
       // value. to do so we need to call AJAX.
       jQuery.ajax({
           url: "path/to/your/php/file/relative/to/this.php",
           data: {"userSelected": userSelected },
           success: function (resp) {
               //At this stage you will get the value
               //that your php file will generate. Then based on the format
               // That your php file is returning you can generate new
               // Select Box.
               console.debug(resp); //This will print your value in Console
               //Let's assume if your php is returning full html select text
               // then you can append that in DOM like this.
               jQuery("body").append(resp);
           }
       });
    }
</script>

3-创建PHP文件。请记住,在步骤2中,url将指向此文件。

现在,在这个文件中,您可以访问userSelected作为

$_REQUEST["userSelected"];

使用这个值从Mysql中获取数据,然后生成一个类似的字符串ab

其中a和b是从MYSQL生成的。你所要做的就是回显这个字符串

echo  "<select><option> a</option><option>b</option>";
相关文章: