如何使用由 AJAX/PHP 填充的 DropDownList 中选定值的值

发布于 2024-08-28 05:11:15 字数 2325 浏览 3 评论 0原文

我有两个下拉列表的表单(假设 A 和 B)。 当我从 A 中选择一个值时,将使用 AJAX 相应地填充 B

在同一页面中,我有一个按钮,按下该按钮时,会将下拉列表中所选项目的值发布到另一个 PHP 页面。我遇到的问题是 B 的选定值返回为空白/空。

有没有办法存储使用 AJAX 填充的下拉列表的选定值?

代码如下:(

主表格)

<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>

填写第二个下拉列表的表格

<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>";     
} ?>

如果您想查看从 下载的完整代码,则 这里

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-09-04 05:11:15

从框 B 中清除旧值,当您将新的选择选项插入到 B 时,也会相应地在选项中插入值。

在js上
注意:argstr 是从 ajax php 文件中获取的字符串,由 value1:option1|value2:option2 分隔
argctrl 获得了之前的 Box B 选项

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;
  }
}

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 js
Note: argstr is string u get from ajax php file seperated by value1:option1|value2:option2
and 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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文