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

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

问题描述

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

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

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

代码如下:

(主表格)

<tr><td width="150"><b>条形:</b></td><td><select name = "bar" onChange="getContact('AssignContactDetailToBar_f_getContacts.php?bar='+this.value)" size = 1 style = "width:190px"><option value = "">---选择---</option><?php而 ($data = mysql_fetch_array($r_getBarsDetails)){echo "<option value="".$data['bar_id']."">".$data['bar_name']." (".$data['town_name'].")</选项>";}?></td></tr><tr><td width="150"><b>联系人:</b></td><td><div id="persondiv"><select name = "person" size = 1 style = "width:190px"><option value = "">--选择栏--</option></td></tr><tr><td><input name="security" type="text" size="15"></td><td><input type="submit" name="Submit" value="Submit"></td></tr></表单>

用于填充第二个下拉列表的表单

<option value = "">--选择人物--</option><?phpwhile($data=mysql_fetch_array($result)){echo "<option value="".$data['person_id']."">".$data['person_name']." ".$data['person_surname']." (".$数据['town_name'].")</option>";?>如果您想从这里查看完整的代码下载 解决方案 清除框 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;清除组合(Ctrl);如果(argstr!=''){for (o=0; o =0; i--){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 js
Note: argstr is string u get from ajax php file seperated by value1:option1|value2:option2
and argctrl got previous Box B optionsfunction 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 中选定值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
                    
相关文章
php问题最新文章 PHP Fatal error: Cannot pass parameter 2 by reference解决办法 PHP Fatal error: Cannot pass parameter N by reference in错误原因及解决办法 在WooCommerce结账中添加带有收集时间的自定义选择字段 在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知 在WooCommerce结账前的强制优惠券(可选:针对特定产品) 根据购物车合计(小计+运费)计算费用,而不将其添加到WooCommerce中的订单总价值 PHP致命错误:无法将词法变量$EventName用作中的参数名 我试图使用PHP代码调用POST请求,但收到了401个未经授权的错误。 在WooCommerce的管理订单编辑页面上获取嵌套自定义字段元数据 根据之前在WooCommerce上的订单,限制客户在同一周内多次购买特定产品 Symfony中的路由异常(&quot;The&quot;/League/:ID_League/Members/new&quot;Routing有一些缺少的必选参数(:ID_League)&quot;) 尝试访问Microsoft Graph时无法获取本地颁发者证书(&Q) 热门教程 HTML/CSS教程JAVASCRIPT教程PHP教程JAVA教程ASP.NET教程Python教程Go教程Ruby教程C教程C++教程
© 2023-2024 沃梦达编程网 版权所有并保留所有权 ICP备案号:粤ICP备14083021号 SiteMap 本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!