Displaying Room availability for a date range in SQL and PHP [duplicate]

人盡茶涼 提交于 2020-06-09 05:39:39

问题


I am trying to display the results of a search query, which is supposed to display available rooms at a hotel between a date range.

Ideally, the results from roomsearchTEST.php should display at the table at the bottom of searchavailabiltyTEST2.php (table id="tblrooms"). Currently nothing is displaying here when I click on submit. Can anyone point me in the right direction?

The current code I have is

searchavailabiltyTEST2.php

<!DOCTYPE HTML>
<html><head><title>Room Availability</title>
<script>

function XMLHttpMethod () {

  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {

      var rooms = JSON.parse(this.responseText);              
      var tbl = document.getElementById("tblrooms"); //find the table in the HTML

      //clear any existing rows from any previous searches
      //if this is not cleared rows will just keep being added
      var rowCount = tbl.rows.length;
      for (var i = 1; i < rowCount; i++) {
         //delete from the top - row 0 is the table header we keep
         tbl.deleteRow(1); 
      }      

      //populate the table
      //rooms.length is the size of our array
      for (var i = 0; i < rooms.length; i++) {
         var rnumber = rooms[i]['roomID'];
         var rname    = rooms[i]['roomname'];
         var rtype   = rooms[i]['roomtype'];
         var beds = rooms[i]['beds'];


         //create a table row with four cells  
         tr = tbl.insertRow(-1);
         var tabCell = tr.insertCell(-1);
             tabCell.innerHTML = rnumber; //roomnumber
         var tabCell = tr.insertCell(-1);
             tabCell.innerHTML = rname; //roomname      
         var tabCell = tr.insertCell(-1);
             tabCell.innerHTML = rtype; //roomtype
         var tabCell = tr.insertCell(-1);
             tabCell.innerHTML = beds; //beds            
        }
    }
  }
  //call our php file that will look for a room availabilty

  var startdate = document.getElementById('checkindate').value;
  var enddate = document.getElementById('checkoutdate').value;
  var msg1 = "&startdate="+startdate+"&enddate="+enddate;

  xmlhttp.open("GET","roomsearchTEST.php"+msg1,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<h1>Search for room Availability</h1>
<h2><a href="/bnb/">[Return to main page]</a>
</h2>

<form id="availability" method="GET" onsubmit="return function XMLHttpMethod ()">
 <p>
    <label for="checkindate">Checkin date: </label>
    <input type="date" id="checkindate" name="checkindate" placeholder="dd-mm-yyyy" required=""> 
  </p>  
  <p>  
    <label for="checkoutdate">Checkout date: </label>
    <input type="date" id="checkoutdate" name="checkoutdate" placeholder="dd-mm-yyyy" required=""> 
</p>
   <input type="submit" id="searchavailability" name="searchavailability" value="Search Availability">
 </form>

<table id="tblrooms" border="1">
<thead><tr><th>Room #</th><th>Room Name</th><th>Room Type</th><th>Beds</th></tr></thead>

</table>
</body>
</html>

And the search function is called from: roomsearchTEST.php

//Our roomsearch/filtering engine
include "config.php"; //load in any variables
$DBC = mysqli_connect("127.0.0.1", DBUSER, DBPASSWORD, DBDATABASE) or die();

$checkindate=$_GET["checkindate"];
$checkoutdate=$_GET["checkoutdate"];
$searchresult = '';

$query="SELECT * FROM `room`  
  WHERE `roomID` NOT IN (SELECT `roomID` FROM `booking` WHERE `checkindate` >= $checkindate AND `checkoutdate` <= $checkoutdate)"
  ;

    $result = mysqli_query($DBC,$query);
    $rowcount = mysqli_num_rows($result); 
        //makes sure we have rooms
    if ($rowcount > 0) {            
        while ($row = mysqli_fetch_assoc($result)) {            
            $searchresult .=$row['roomID'].','.$row['roomname'].','.$row['roomtype'].','.$row['beds']."|"; 
        }
    } else {$searchresult.= "None";} 

mysqli_free_result($result); //free any memory used by the query
mysqli_close($DBC); //close the connection once done
$searchresult=substr($searchresult,0,strlen($searchresult)-1);
echo  $searchresult;

?>

Let me know if any more information is required

Thanks

来源:https://stackoverflow.com/questions/62012424/displaying-room-availability-for-a-date-range-in-sql-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!