如何从使用AJAX / PHP填充的DropDownList中使用所选值的值 [英] How to use the Value of a Selected Value from a DropDownList populated with AJAX/PHP
问题描述
当我从A中选择一个值时,B正在使用AJAX相应填充
在同一页面中,我有一个按钮,当按下时,发布值的所选项目的下拉列表到另一个PHP页面。我遇到的问题是B的选定值返回为空白/空白。
有没有办法存储使用AJAX填充的下拉列表的选定值?
代码如下:
(主FORM)
< form name =NewBarmethod =postonsubmit =return validateFormOnSubmit(this)action =AssignContactDetailToBar_f.php>
< tr>
< td width =150>< b> Bar:< / b>< / td>
< td>
< select name =baronChange =getContact('AssignContactDetailToBar_f_getContacts.php?bar ='+ this.value)size = 1 style =width:190px>
< option value => ---选择---< / 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>联系人:< / b>< / td>
< td>
< div id =persondiv>< select name =personsize = 1 style =width:190px>
< option value => - 选择栏 - < / option>
< / td>
< / tr>
< tr>
< td>
< input name =securitytype =textsize =15>
< / td>
< td>
< input type =submitname =Submitvalue =Submit>
< / td>
< / tr>
< / form>
FORM填充第二个下拉列表
< select name =personsize = 1 style =width:190px>
< option value => - 选择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>;
}?>
如果你想看到从 here 完整的代码下载
从框B中清除旧值,当您将新的选择选项插入到B中时,也会相应地在选项中插入值。
on 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 ]。分裂(:);
ctrl [ctrl.length] = new Option(splitarr [1],splitarr [0]);
}
}
}
function clearcombo(argctrl)
{
for(var i = argctrl.length-1; i> = 0; i--)
{
argctrl [i] = null;
}
}
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 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中使用所选值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!