views:

977

answers:

3

I have the code below that will change a state dropdown list when you change the country list.

How can I make it change the state list ONLY when country ID number 2234 and 224 are selected?

If another country is selected is should change into this text input box

<input type="text" name="othstate" value="" class="textBox">

The form

<form method="post" name="form1">
<select style="background-color: #ffffa0" name="country" onchange="getState(this.value)">
<option>Select Country</option>
<option value="223">USA</option>
<option value="224">Canada</option>
<option value="225">England</option>
<option value="226">Ireland</option>
</select>

<select style="background-color: #ffffa0" name="state">
<option>Select Country First</option>
</select>

The javascript

<script>
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);
   }
}
</script>
+1  A: 

Just check the countryId value before you do the AJAX request and only perform the request if the countryId is in the allowable range. In the case where the countryId doesn't match, I would hide the select (probably clear it's value, too) and show an already existing input that was previously hidden. The reverse should be done if an allowable country is chosen.

jQuery example below:

<form method="post" name="form1">
   <select style="background-color: #ffffa0" name="country" onchange="getState(this.value)">
      <option>Select Country</option>
      <option value="223">USA</option>
      <option value="224">Canada</option>
      <option value="225">England</option>
      <option value="226">Ireland</option>
   </select>

   <select style="background-color: #ffffa0" name="state">
      <option>Select Country First</option>
   </select>

   <input type="text" name="othstate" value="" class="textBox" style="display: none;">
</form>

$(function() {
    $('#country').change( function() {
        var val = $(this).val();
        if (val == 223 || val == 224) {
            $('#othstate').val('').hide();
            $.ajax({
               url: 'findState.php',
               dataType: 'html',
               data: { country : val },
               success: function(data) {
                   $('#state').html( data );
               }
            });
        }
        else {
           $('#state').val('').hide();
           $('#othstate').show();
        }
    });
});
tvanfosson
I follow the theory I just don't know enough javascript to write it, im a php man
jasondavis
You're better off using jQuery anyway. I've added a jQuery example.
tvanfosson
I like this as I already have jquery loaded on all pages,The example code gives me this error $ is not defined
jasondavis
Another though instead of loading a new page with states, if I were to have the states list already on the page could it be hidden and then shown in the same manner?
jasondavis
Also getState is not defined
jasondavis
Yes. If the states were already on the page, you could show/hide those although that would get tricky if you expanded to more than two countries with states. You'd also have to be careful how you named them or change the names when making them visible/invisible.
tvanfosson
You'd need to include this code **after** loading jQuery. If not, you'll get the '$ is not defined error.' getState was your function. You probably left the reference to it in your HTML and it is no longer there.
tvanfosson
A: 

I think the simple thing to do is to provide a state dropdown and a text entry box with different ids. Set the display of both to none and then you just need to surround your contents of getState() with

if (countryId == 233 || countryId == 234) {
   /* Ajax state population here */

   dropdownId.display = 'block';
   textEntryId.display = 'none';
}
else  {
   textEntryId.display = 'block';
   dropdownId.display = 'none';
}

(where dropdownId and textEntryId are the ids of the relevant UI components) so you enable/display the display for the state dropdown or the text entry upon selection.

JQuery is all well and good, but I wouldn't introduce it just to solve this problem.

Brian Agnew
A: 

How can we populate state combobox from database on selection of country from country combobox which at first is populated from database

neha