I have two files SearchHotelUsingXMLhttp.html and other file is getHotelDetails.php, which is located in webservices which is a directory. Now for given hotel search I created one button search, from search button I am calling java script function searchHotel(str). From this javacript function I am calling getHotelDetails.php using xmlHttp request, but I am getting the following message.
Notice: Undefined index: q in C:\xampp\htdocs\Experiements\webservices\getHotelDetails.php on line 2
city name
SearchHotelUsingXMLhttp.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<script type="text/javascript">
function searchHotel(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST","webservices/getHotelDetails.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<h1>Hotels</h1>
<form>
City: <select name="hotel_city" >
<option value="Bangalore"> Bangalore</option>
<option value="Chennai"> Chennai</option>
</select>
<br/><br/>
<input type="button" id="myBtn" value="Search Hotel" onclick="searchHotel(this.value)">
<!--
<input type="button" id="myBtn" value="Search Hotel" onclick="searchHotel(this.value)">
-->
</form>
<br>
<div id="txtHint"><b>Hotel info will be listed here.</b></div>
</body>
</html>
getHotelDetails.php
<?php
$q = $_POST['q'];
$con = mysqli_connect('localhost','root','3456','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM hotel WHERE city = '$q'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>city</th>
<th>name</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>