本文介绍了如何使用所选择的价值论,从填充了AJAX / PHP的一个DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个dropdownlists形式(可以说A和B)。当我选择从A值,B被填充相应的使用AJAX

在同一页我有一个按钮,当pressed,职位dropdownlists的所选项目到另一个PHP页面的价值。我遇到的问题是,B的选择的值返回为空/空。

有没有一种方法来存储使用AJAX填充DropDownList的选定值?

低于code:

(主要形式)

 <表格名称=NewBar方法=邮报的onsubmit =返回validateFormOnSubmit(本)行动=AssignContactDetailToBar_f.php>
           &其中; TR>
                < TD WIDTH =150>< B>酒吧:LT; / B>< / TD>
                < TD>
                    <选择名称=酒吧的onChange =getContact('AssignContactDetailToBar_f_getContacts.php栏=?'+ THIS.VALUE)大小= 1风格=宽度:190px>
                    <期权价值=> ---选择---< /选项>
                    < PHP
                    而($数据= mysql_fetch_array($ r_getBarsDetails))
                    {
                        回声<期权价值= \$数据['bar_id']\>。$数据['bar_name'](。$数据['town_name'。)< /选项>中;
                    }
                    ?>
                < / TD>
            < / TR>
            &其中; TR>
                < TD WIDTH =150>< B>联系人:LT; / B>< / TD>
                < TD>
                    < D​​IV ID =persondiv><选择名称=人大小= 1风格=宽度:190px>
                    <期权价值=>  - 选择酒吧 - < /选项>
                < / TD>
            < / TR>
            &其中; TR>
                < TD>
                    <输入名称=安全类型=文字大小=15>
                < / TD>
                < TD>
                    <输入类型=提交名称=提交值=提交>
                < / TD>
            < / TR>
        < /形式GT;
 

FORM来填充第二DROPDOWNLIST

 <选择名称=人大小= 1风格=宽度:190px>
<期权价值=>  - 选择人物 - < /选项>
< PHP
而($数据= mysql_fetch_array($结果))
{
    回声<期权价值= \$数据['PERSON_ID']\>。。$数据['PERSON_NAME']$数据['person_surname']($数据['town_name'])< /选项>中。
?}>
 

如果你想看到来自这里

解决方案

清除从盒B中的旧值,虽然u的插入新的选择选项也b在相应的选项中插入的值。

在JS注:argstr是字符串ü从值1分隔AJAX php文件获得:选项|数值2:选项和argctrl了previous乙栏选项

 函数splitstr(argstr,argctrl)
{
变种O;
    VAR CTRL =的eval(argctrl);
    VAR prezar = argstr.split(|);
    argctrl.length = 0;
    clearcombo(CTRL);
    如果(argstr!='')
    {
        对于(O = 0; O< prezar.length;○++)
        {
          splitarr = prezar [O] .split(:);

           CTRL [ctrl.length] =新的选项(splitarr [1],splitarr [0]);

        }
    }
}


功能clearcombo(argctrl)
{
  对于(VAR I = argctrl.length-1; ​​I> = 0;我 - )
  {
    argctrl [I] =无效;
  }
}
 

I have form with two dropdownlists (lets say A and B).When I select a value from A, B is being populated accordingly using AJAX

In the same page I have a button, that when pressed, posts the values of the selected items of the dropdownlists to another PHP page. The problem I am having is that the selected value of B is returned as Blank/Empty.

Is there a way to store the selected value of a dropdownlist populated using AJAX?

Code below:

(Main FORM)

<form name="NewBar" method="post" onsubmit="return validateFormOnSubmit(this)" action="AssignContactDetailToBar_f.php">
           <tr>
                <td width="150"><b>Bar:</b></td>
                <td>
                    <select name = "bar" onChange="getContact('AssignContactDetailToBar_f_getContacts.php?bar='+this.value)" size = 1 style = "width:190px">
                    <option value = "">---Select---</option>
                    <?php
                    while ($data = mysql_fetch_array($r_getBarsDetails))
                    {
                        echo "<option value=\"".$data['bar_id']."\">".$data['bar_name']." (".$data['town_name'].")</option>";
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td width="150"><b>Contact Person:</b></td>
                <td>
                    <div id="persondiv"><select name = "person" size = 1 style = "width:190px">
                    <option value = "">--Select Bar--</option>
                </td>
            </tr>
            <tr>
                <td>
                    <input name="security" type="text" size="15">
                </td>
                <td>
                    <input type="submit" name="Submit" value="Submit">
                </td>
            </tr>
        </form>

FORM to populate the 2nd Dropdownlist

<select name="person" size = 1 style = "width:190px">
<option value = "">--Select Person--</option>
<?php
while($data=mysql_fetch_array($result))
{
    echo "<option value=\"".$data['person_id']."\">".$data['person_name']." ".$data['person_surname']." (".$data['town_name'].")</option>";
} ?>

if you would like to see the complete code download from here

解决方案

Clear the old value from box B and while u are inserting new select options to B also insert the values in the options accordingly.

on jsNote: argstr is string u get from ajax php file seperated by value1:option1|value2:option2and argctrl got previous Box B options

function splitstr(argstr,argctrl)
{
var o;
    var ctrl = eval(argctrl);
    var prezar = argstr.split("|");
    argctrl.length = 0;
    clearcombo(ctrl);
    if(argstr!='')
    {
        for (o=0; o < prezar.length; o++)
        {
          splitarr = prezar[o].split(":");

           ctrl[ctrl.length] = new Option(splitarr[1], splitarr[0]);

        }
    }
}


function clearcombo(argctrl)
{
  for (var i=argctrl.length-1; i>=0; i--)
  {
    argctrl[i] = null;
  }
}

这篇关于如何使用所选择的价值论,从填充了AJAX / PHP的一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 04:32