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:
- <tr>
- <td valign="middle">Country</td>
- <td valign="middle">:</td>
- <td valign="middle">
- <select name="cboCountry" id="cboCountry" style="width: 100%" required>
- <option value="">[Country..]</option>
- <?php
- $mysql_command = "CALL sp_populate_country()";
- $mysql_query = $mysql_connection->prepare($mysql_command);
- $mysql_query->execute();
- while($mysql_row = $mysql_query->fetch())
- {
- ?>
- <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>
- <?php } ?>
- </select>
- </td>
- </tr>
- <tr>
- <td valign="middle">State / City</td>
- <td valign="middle">:</td>
- <td valign="middle">
- <img id="imgLoading" src="<?php echo $_SESSION["domain_name"]; ?>images/loading_20X20.gif" alt="XoomPage" hidden>
- <select name="cboRegion" id="cboRegion" style="width: 100%">
- <option value="">[State / City..]</option>
- </select>
- </td>
- </tr>
- $("#cboCountry").change(function() {
- // var country = $(this).find("option:selected").val();
- $("#cboRegion").hide();
- $("#imgLoading").show();
- // alert(country);
- $.ajax({
- url : "populate_region.php?id=" + $(this).val(),
- type: 'GET',
- dataType:'json',
- cache: false,
- success : function(data) {
- if (data.success) {
- $("#cboRegion").html(data.options);
- $("#imgLoading").hide();
- $("#cboRegion").show();
- }
- else
- {
- alert("Error! Unable to populate states / cities!");
- }
- }
- });
- });