views:

351

answers:

2

Hi,

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

+1  A: 

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;
  }
}
nik
thanks nik, but I cannot understand your answer ..... if you want you can mail me on [email protected]. thanks
mouthpiec
hmmmmm, i saw ur files, but my manner of getting option values is different, Instead of echoing whole thing u can use just values and options in specific manner like value:option|value:optionU will get this as responseTextThen u can call this function: splitstr(responseText,"document.NewBar.persondiv.options");
nik
thanks nik, i will try to figure out how to do it. but i think that since the dynamic list is on a seperate php page, its values are being lost :(
mouthpiec
A: 

Hello Programming Experts,
To Nik,
I am facing same problem.I am generating two dropdown using ajax. But I can't access value of those dropdowns to another page. i cannot understand this code..what should i do to get value from those dropdowns....Where should i put this function?
Plz reply me at [email protected]....

Thanks
Chintan

Chintan