I have a script online hotel reservation system where the prices for rooms and room types are derived from the database using php. I have only 2 rooms in this system. One accommodates up to 4 people, the other - up to 6 people. The reservation form has From and To Date, Room Type (x suite), which can be selected from 0 to 1, and then the price is x 120 per room per night. I also have the opportunity to choose the number of People. The established price of 120 per room per day is designed for 2 people. For each additional person 20 is added to the price. therefore, if there are 3 people in the suite, the price will be 140. I tried to make it work on my own, but I just can’t make the price change according to the number of people selected. The code for the select tag for people is as follows:
<select name="people" class="FormFields" onChange="checkBookingForm()" style="width:70px">
<?php for ($i=1; $i<=$SETTINGS["room_people"]; $i++) {
if ($i==$_REQUEST["people"]) {
$selected='selected';
}
else {
$selected='';
}
?>
<option value="<?php echo $i; ?>" <?php echo $selected; ?>><?php echo $i; ?></option>
<?php }; ?>
</select>
The checkBookingForm () function deals with changing the text when a certain room is selected, and the number of people who hold this room will be displayed. function here:
<script language="javascript" type="text/javascript">
function checkBookingForm () {var allRooms = new Array (); var totalPeople = 0;
for(i=0; i<document.NewBookingFrm.elements.length; i++) {
if(document.NewBookingFrm.elements[i].type=="select-one") {
name_t = document.NewBookingFrm.elements[i].name;
if (name_t!=='people') {
value_t = document.NewBookingFrm.elements[i].value;
totalPeople = parseInt(allRooms[name_t]) * parseInt(value_t) + totalPeople;
}
}
}
if (totalPeople==1) {
var acP = 'person';
} else {
var acP = 'people';
};
if (totalPeople!=document.NewBookingFrm.people.value) {
totalPeople = '<strong style="color:red; font-size:14px">'+totalPeople+'</strong>'
}
document.getElementById('PeopleAccommodate').innerHTML = 'selected room(s) can accommodate ' + totalPeople + ' ' + acP;
}
I tried different things, but as I learn Javascript at the same time, I will not go anywhere. I'm still trying to understand how Dom works. If anyone can point me in the right direction , it will be really appreciated. I understand what I have to do with the code:
- first check how many people are selected
- then check (if the value is 1)
- and if one room is selected, increase the price accordingly, reduce it.
for the number of rooms available I currently have this code:
<select name="room_<?php echo ReadFromDB($row["id"]); ?>" class="FormFields" onChange="checkBookingForm()" style="width:70px">
<option value="0">0</option>
<?php for ($i=1; $i<=$available_rooms; $i++) {
if ($i==$booked_rooms["quantity"]) $selected=' selected'; else $selected='';
?>
<option value="<?php echo $i; ?>"<?php echo $selected; ?>><?php echo $i; ?></option>
<?php }; ?>
</select>
x
">link to more complete code: http://jsfiddle.net/x6ZYB/