I want to change the values of select box based on the value of text box I'm using jquery autocomplete to display the suggestions in a list and when I select a suggestion it becomes the value of text box now I want to display the names of diff states based on the value of the text box.
My code:
<div class="form-group">
<label>Country*</label>
<span class="span_error"><?=$error_username;?></span>
<input name="emp_country" type="text" class="form-control" id="emp_country" placeholder="Employee Country" value="" onchange="getstate(this.value);">
</div>
<div id="countryList"></div>
</div>
I m generating autocomplete here n displaying the suggestions in Country list and this is working great.
Selectbox HTML:
<div class="form-group">
<label>State*</label>
<span class="span_error"><?=$error_username;?></span>
<select name="ddl_state" size="1" class="form-control" id="ddl_state">
<option value="" disabled='' selected=''>Select State</option>
</select>
</div>
I want to display the names of diff states of the Country name given in the above text box
Javascript:
function getstate(contry)
{
alert(contry);
$.ajax({
type:"POST",
url:"getdata.php",
data:{contry:contry},
success:function(data) {
$('#ddl_state').html(data);
}
});
}
PHP:
if (isset($_POST['contry'])) {
$contry = $_POST['contry'];
get_country_state($contry);
}
function get_country_state($contry)
{
$sql = "Select count_id from countries where count_name='$contry'LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array(result);
$country_id = $row['count_id'];
$sql_state = "select * from states where count_id='$country_id'ORDER BY state_name";
$result_state = mysql_query($sql_state);
if ($result_state) {
// code...
$output='<option value="">Select State</option>';
while ($ro = mysql_fetch_array($result_state)) {
// code...
$output .= '<option value="' . $ro["state_id"] .'">'.$ro["state_name"] . '</option>';
}
echo $output;
} else {
echo mysql_error($db);
}
}