I have a select option from which I can select a hotel name which I get from a php script.
And then I have another select option which shows room types based on the hotel selected from 1st select option.
And when I select a hotel with the help of ajax I only get one room type in my 2nd select option, while in my table I have multiple room types for a single hotel.
My php code for getting room types.
<?php
include('mysql.php');
$h_id = $_POST['hotel_id'];
$result = mysql_query("SELECT * FROM room_type WHERE hotel_id = '$h_id'");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$type_name = $row['type_name'];
$type_id = $row['roomtype_id'];
echo $type_name.",".$type_id;
}
exit();
?>
javascript:
jQuery(document).ready(function($){
$('#hotel_list').change(function(){
$.ajax({
type:'post',
url:'roomtype_fetch.php',
data: 'hotel_id='+ $(this).val(),
success: function(value){
var data = value.split(",");
var type_name =data[0];
var type_id =data[1];
$("#roomtype_list").html("<option value="+type_id+">"+type_name+"</option>");
}
});
});
});
html for 1st select option with its php to get hotel name.
<select class="form-control" name="hotel_list" id="hotel_list" onchange="cal()">
<option>--Select--</option>
<?php
$query2 = mysql_query("SELECT * FROM hotel") or die("the query cannot be completed at this moment");
if(mysql_num_rows($query2) <1) {
?>
<option>No Hotel Found!</option>
<?php
}
while($row = mysql_fetch_array($query2, MYSQL_ASSOC)){
$hotel_name = $row['hotel_name'];
$hotel_id_1 = $row['hotel_id'];
?>
<option value="<?php echo $hotel_id_1; ?>"><?php echo $hotel_name; ?></option>
<?php
}
?>
</select>
2nd select html code:
<select class="form-control" name="roomtype_list" id="roomtype_list">
<option>--Select--</option>
</select>
Any type of help would be appreciated.