I want to display the echoed text from my PHP file into a 'p' text in my main html page (main.php)
I connect to a database to retrieve the data I want and in that PHP page, I currently have it to echo a text depending on what data is available in the database.
Right now, in my main.php page I have a button called "Search" which redirects me to another availability.php page that displays a echo message telling me my result. I want that message to display in my main page without refreshing the page.
Following is my PHP file.
<?php
session_start();
include "php/db.php";
/////////////////////////////////////////////////////
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$date1=date_create("$checkin");
$date2=date_create("$checkout");
$diff=date_diff($date1,$date2) ->format ("%a");
$nightsStaying = (int) ("$diff");
////////////////////////////////////////////////////
$peopleString = $_POST['people'];
$people = (int) "$peopleString"; //converts people string to int
////////////////////////////////////////////////////
if ($people > 1){
$results = $db->query("SELECT * FROM availability WHERE date >= '$checkin' AND date < '$checkout' AND (droom > 0 OR suite > 0) ")->fetchALL(PDO::FETCH_ASSOC);
// Checks if a double room or suite are available on the days requested
$resultsCount = count($results); //checks how many rows are retrieved
if ($resultsCount == $nightsStaying){
echo "There are available rooms";
}else{
echo "There is no availability for the selected dates";
}
}
else {
$results = $db->query("SELECT * FROM availability WHERE date >= '$checkin' AND date < '$checkout' AND (sroom > 0 OR droom > 0 OR suite > 0) ")->fetchALL(PDO::FETCH_ASSOC);
// CHECK FOR ANY ROOM IS GREATER THAN 0.
$resultsCount = count($results);
if ($resultsCount == $nightsStaying){
echo "There are available rooms";
}else{
echo "There is no availability for the selected dates";
}
}
?>
HTML part:
<form id="availabilitySearch" action="availability.php" method="post">
<div class="d-flex justify-content-center container">
<div class="col m2">
<label><i class="fa fa-calendar-o"></i> Check In</label>
<input id="checkin" class="input border" name="checkin" type="date">
</div>
<div class="col m2">
<label><i class="fa fa-calendar-o"></i> Check Out</label>
<input id="checkout" class="input border" name="checkout" type="date">
</div>
<div class="col m2">
<label><i class="fa fa-male"></i> People</label>
<input id="people" class="input border" name="people" type="number" placeholder="1">
</div>
<div class="col m2">
<label><i class="fa fa-search"></i> Search</label>
<button type="submit" class="button block black">Search</button>
</div>
</div>
</form>
<div>
<!-- THIS IS WHERE I WANT THE AJAX SCRIPT TO SHOW THE RESULT-->
<p></p>
</div>