how to select <option> value after loading with ajax

how to select <option> value after loading with ajax

Hi,

I am using jquery to populate cities from selected country in a <select> element using the below code. This working perfect when opening it for a new company record. what if I want to edit existing record by detecting the $_GET["id"] of the company? I am setting the cboCountry value to $company_country which is a value from MySQL.. now how can I populate the modify below code to populate the cities for the selected country ($company_country) and also make the selected city = $company_city.

Hope my question is clear.. with thanks,,

here is the code:

  1. <tr>
  2.     <td valign="middle">Country</td>
  3.     <td valign="middle">:</td>
  4.     <td valign="middle">
  5.         <select name="cboCountry" id="cboCountry" style="width: 100%" required>
  6.             <option value="">[Country..]</option>
  7.             <?php
  8.                 $mysql_command = "CALL sp_populate_country()";
  9.                 $mysql_query = $mysql_connection->prepare($mysql_command);

  10.                 $mysql_query->execute();

  11.                 while($mysql_row = $mysql_query->fetch())
  12.                 {
  13.             ?>
  14.             <option value="<?php echo $mysql_row['country_code_alpha2']; ?>" <?php if ($mysql_row['country_code_alpha2'] == $company_country) { echo 'selected'; } ?>><?php echo $mysql_row['country_name']; ?></option>
  15.             <?php } ?>
  16.         </select>
  17.     </td>
  18. </tr>
  19. <tr>
  20.     <td valign="middle">State / City</td>
  21.     <td valign="middle">:</td>
  22.     <td valign="middle">
  23.         <img id="imgLoading" src="<?php echo $_SESSION["domain_name"]; ?>images/loading_20X20.gif" alt="XoomPage" hidden>
  24.         <select name="cboRegion" id="cboRegion" style="width: 100%">
  25.             <option value="">[State / City..]</option>
  26.         </select>
  27.     </td>
  28. </tr>


  1.     $("#cboCountry").change(function() {
  2.         // var country = $(this).find("option:selected").val();
  3.         $("#cboRegion").hide();
  4.         $("#imgLoading").show();
  5.         // alert(country);
  6.         $.ajax({
  7.         url : "populate_region.php?id=" + $(this).val(),
  8.         type: 'GET',
  9.         dataType:'json',
  10.         cache: false,
  11.         success : function(data) {
  12.             if (data.success) {
  13.                 $("#cboRegion").html(data.options);
  14.                 $("#imgLoading").hide();
  15.                 $("#cboRegion").show();
  16.             }
  17.             else
  18.             {
  19.                 alert("Error! Unable to populate states / cities!");
  20.             }
  21.         }
  22.     });
  23. });