0

I have two dropdown/select input on a form, Room Type and Number of Guests both of them are connected to mysql.

How do I make the value of my Number of guests change automatically to whatever data is connected to the Room Type.

For example, Room Type A can only have a maximum of 2 guests and on mysql there is a table for room types which have the number of rooms and number of guests limits.

How do I echo the guests limit into the number of guests input form?

Here's my ROOM TYPE code:

<h5>Room Type</h5>

    <select id="category" name="category" required="">

      <?php
      include 'connect.php';
      $room= "SELECT * FROM roomtype";
       $resroom = $conn->query($room);
        while($roomtype = $resroom->fetch_assoc()){
        ?>
      <option value="<?php echo $roomtype['roomtype'];?>" ><?php echo $roomtype['roomtype'];?></option>

        <?php 
        $max = $roomtype['guests'];
        }?>
        </select>

Here's my Number of guests code:

<h5>Number of Guests *</h5>
<select id="category1" name="guests" required="">
 <?php
 include 'connect.php';
 $inc = 1;
 if(isset($_POST))    
 {
 $category = $_POST['category']; 
 }
 $guest = "SELECT * FROM roomtype WHERE roomtype = $category";
  $g = $conn->query($guest);
   while($guest1 = $g->fetch_assoc()){
 while ($guest1 ['guests'] < 0){
  ?>
  <option value="1"><?php echo $inc; $inc = $inc + 1; }}?></option>
   </select>

My connect/database code:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "booking";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>

The roomtypetable has three columns id, roomtype, guests.

E. Cristoph
  • 43
  • 10
  • You need to use ajax – GYaN Dec 01 '17 at 07:22
  • What would be the correct code if I use Ajax? Can you point me to a tutorial or similar question? Thanks! – E. Cristoph Dec 01 '17 at 07:23
  • Tell me how you defined the number of guests in every room..? – GYaN Dec 01 '17 at 07:25
  • The number of rooms are already registered in the roomtype database table under the booking database . RoomType A is only 2 maximum rooms, RoomType B is only 3 maximum rooms, etc. – E. Cristoph Dec 01 '17 at 07:27
  • NO.. I want to ask how the system get to know that this room's limit is this.. You are entering the number of guests for every room in database. Correct..? – GYaN Dec 01 '17 at 07:29
  • No. The limit for the number of guests is already with the RoomType. So if they pick, for example, Room Type A, I'd want the dropdown/select forms to take the values that is already added in the booking database. I'm not adding new value to the table but rather inserting them. – E. Cristoph Dec 01 '17 at 07:33
  • wait... I'm giving you the answer,, – GYaN Dec 01 '17 at 07:33

2 Answers2

0

You need to achieve the same with the help of JQuery instead of php. because this operation will happen at the client side.

What you can do is add a comman attr in both select option named value with the id you will fetch from mysql.

Aside this you need to handle onselect event you need to change the value in numberofguest.

for more details : how to change a selections options based on another select option selected?

Shivang Agarwal
  • 1,825
  • 1
  • 14
  • 19
0

First you have to select room data with number of guests of every room..

Now code them like this

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h5>Room Type</h5>
<select id="category" name="category" required="">
    <option value="0">Room Number</option>
    <?php 
        include 'connect.php';
        $room= "SELECT * FROM roomtype";
        $resroom = $conn->query($room);
        while($roomtype = $resroom->fetch_assoc())echo '
    <option value="'.$roomtype['roomtype'].'" data-limit="'.$roomtype['guests'].'" >'.$roomtype['roomtype'].'</option>';
    ?>
</select><br>
<h5>Number of Guests</h5>
<select id="guests" name="guest"><option value="">Number of Guests</option></select>

jQuery

<script>
    $('#category').change(function(e) {
        var limit = parseInt($(this).find(':selected').data('limit')); //get data-limit of selected option
        var html = '<option value="">Number of Guests</option>';
        for(i=1;i<=limit;i++)html = html+'<option value="'+i+'">'+i+' Guest(s)</option>'; //this loop run from 1 to max numbers of guest
        $('#guests').html(html);
    });
</script>
GYaN
  • 2,327
  • 4
  • 19
  • 39