tags:

views:

321

answers:

4

Hi all,

I have this problems. using html and php.

May I know how to do this. I have 2 drop down, eg A and B. Drop down B is depend to the drop down A. Example, A have these options which will be called from dbase(no prob with this, tq) (Jack, Carol), and B wil have options depend on A: if select Jack(T1, T2, T3), if select carol(T1,T2,T3,T4,T5).

Here are the sample interface.

alt text

Can someone help me with this?

thank you.

+1  A: 

U need to Work with Ajax In this Case. Without Refreshing the Page Selecting any of the A column will give u corresponding B column Value. For Example

<form method="post" name="form1">
 <table border="0" cellpadding="0" cellspacing="0" width="60%"><tbody>
  <tr>
   <td width="150">Country</td>
   <td width="150"><select style="background-color: #ffffa0" name="country" onchange="getState(this.value)"><option>Select Country</option><option value="1">USA</option><option value="2">Canada</option>       </select></td>
  </tr>
 <tr>
  <td>State</td>
  <td>
  <p id="statediv">
  <select style="background-color: #ffffa0" name="state"><option>Select Country First</option>       </select></td>
</tr>
<tr>
  <td>City</td>
  <td>
  <p id="citydiv">
  <select style="background-color: #ffffa0" name="city"><option>Select State First</option>       </select></td>
</tr>
</tbody></table>
</form>

As you can see above, in the onChage event of the country drop down getState() function of the javascript is called which change the options values the State drop down, let’s look at the code the getState() function.

function getState(countryId)
{
   var strURL="findState.php?country="+countryId;
   var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
      if (req.readyState == 4)
      {
     // only if "OK"
     if (req.status == 200)
         {
        document.getElementById('statediv').innerHTML=req.responseText;
     } else {
       alert("There was a problem while using XMLHTTP:\n" + req.statusText);
     }
       }
      }
   req.open("GET", strURL, true);
   req.send(null);
   }
}

The code of the PHP file findState.php, which populate the options in the drop down of the state which is fetched from Ajax , is given below

<? $country=intval($_GET['country']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,statename FROM state WHERE countryid='$country'";
$result=mysql_query($query);

?>
<select name="state" onchange="getCity(<?=$country?>,this.value)">
 <option>Select State</option>
  <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['id']?>><?=$row['statename']?></option>
  <? } ?>
</select>

In the above state dropdown, getCity() function is called in onChage event with countryId and stateId parameter, now let’s look at the code of the getCity() function

function getCity(countryId,stateId)
{
  var strURL="findCity.php?country="+countryId+"&state="+stateId;
  var req = getXMLHTTP();
  if (req)
  {
    req.onreadystatechange = function()
    {
      if (req.readyState == 4) // only if "OK"
      {
        if (req.status == 200)
        {
          document.getElementById('citydiv').innerHTML=req.responseText;
        } else {
          alert("There was a problem while using XMLHTTP:\n" + req.statusText);
        }
      }
    }
    req.open("GET", strURL, true);
    req.send(null);
  }
}

In the above ajax function, findcity.php is called and this PHP file populate the city dropdown according to the supplied parameters country and state from get method. Now let’s look at the code of findcity.php,

<?php $countryId=intval($_GET['country']);
$stateId=intval($_GET['state']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";
$result=mysql_query($query);

?>
<select name="city">
 <option>Select City</option>
  <?php while($row=mysql_fetch_array($result)) { ?>
 <option value><?=$row['city']?></option>
<?php } ?>
</select>

And thats all, the triple drop down list of city, country and state using Ajax and PHP will be populated.

OM The Eternity
I Recommend Ajax To Avoid PAge Refresh....
OM The Eternity
i recommend jquery to avoid all that javascript code
Galen
Thanx all but i think i'll look on jquery first..ajax too alien for me...any more suggestion??
Try it the above code .. its will help.. otherwise u can try for Jquery looklike "prototype" as well...
OM The Eternity
A: 

Ajax is "cool" thing right now so everyone is going to say use ajax. I'm going to say you don't NEED ajax. An alternative would be to disable the second form. When the first select is submitted/changed the page reloads adding something to the querystring e.g. ?a=jack and the second select is populated with the correct values.

Now any pages linking to ?a=jack will automatically populate the second select, when with ajax youd have to write even more javascript code.

I'm not saying don't use ajax, im just giving a different, most likely easier alternative.

Only use ajax when it really helps the user experience.

Galen
@Galen: You would necessarily have to click the Submit button every time you want to change the selection in Drop Down A to populate Drop Down B. I would not consider this 'standard' webform design in any way.
manyxcxi
No you wouldn't. You could put a function in the onchange event of the first select that loads the necessary page. Even if you did have to hit a button, it would be perfectly usable.
Galen
@Galen As u said "Ajax is "cool" thing right now so everyone is going to say use ajax" I think Jquery is the thing he/she needs to go through it proeperly before use, its syntax, its use, everything...
OM The Eternity
Guess What We three are arguing for the question Whose Owner is not at all bothered, I dont know whether he/she has even tracked the tracked his/her question or not, BUT there is no arguement in jquery and Ajax as its all developers choice, what he/she finds it useful and easily useable
OM The Eternity
@Galen: You are correct in that you don't NEED AJAX, but this is the most common use case I can think of were using AJAX isn't frivolous. I will concede that you could structure your workflow so that clicking a Submit/Next/Apply/Whatever button wouldn't seem illogical
manyxcxi
@Parth: I would argue that jQuery is, at it's most basic, a library for AJAX in this case. You could also use Prototype or write your own call in Javascript, but they are all doing the same thing.
manyxcxi
A: 

You will need Ajax bound to the onClick of Drop Down A that is set to populate the inner HTML of Drop Down B

manyxcxi
you don't NEED ajax
Galen
Sorry for the late response. Im not very familiar with AjaX..Is there other way to do beside ajax??
A: 

ONE way to do it if you absolutely do not want to use jQuery nor javascript:

<form action="?step=2" method="post">
<select name="name">
    <option if($_POST['name'] == 'Jack'){echo ' selected="selected" ';}>Jack</option>
    <optionif($_POST['name'] == 'Carol'){echo ' selected="selected" ';}>Carol</option>
</select>

<?php if($_GET['step'] == 2){?>
<select name="b">
<option>1</option>
<option>2</option>
<option>3</option>
<?php if($_POST['name'] == 'Carol'){?>
<option>4</option>
<option>5</option>
<?php } ?>
</select>
</form>
<?php } ?>

<input type="submit" name="next" value="Next" />

You will have to breack the form/page into steps. And the whole form needs to be re-populated as the pages are reloaded.

Hope this can be of assistance to you

John
Further I really recommend learning a bit of javascript for tasks like this. And please, next time could you perhaps post at least the html. I think you would get more answers.
John