-1

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);
?> 

my database image

Neelabh Singh
  • 2,600
  • 12
  • 51
  • 88
  • What is given when you output POST? Also, where do you declare xmlhttp outside of the if statements? – David Dec 24 '14 at 12:46
  • Try changing line 2 to: $q = $_GET['q']; It looks like you're setting it as post but instead sending GET parameters. (Also your script is vulnerable to SQL Injection.) – Jack Dec 24 '14 at 12:49
  • Yeah try changing the $_POST to $_GET to get the query string params – Benison Sam Dec 24 '14 at 12:49
  • @Benison Sam I need Post method, So How I will do that? – Neelabh Singh Dec 24 '14 at 12:50
  • You can access the query string using the $_GET and the form submitted values using $_POST. So use them accordingly. – Benison Sam Dec 24 '14 at 12:53
  • Hi @jack-nicholson, How I am sending the GET parameter, please clarify it. xmlhttp.open("POST","webservices/getHotelDetails.php?q="+str,true); – Neelabh Singh Dec 24 '14 at 12:54
  • This part is sending the get parameters: ?q="+str – Jack Dec 24 '14 at 12:54
  • The "q" you are passing in the query string is considered to be a GET param while it is processed at the server end using php. So you can access it using $q = $_GET['q'] – Benison Sam Dec 24 '14 at 12:55
  • @jack-nicholson, thanks for your reply, but how I will do that for post? – Neelabh Singh Dec 24 '14 at 12:56
  • See newly posted answer by Jeroen. Also, do fix your SQL query to not be vulnerable to SQL Injection. – Jack Dec 24 '14 at 12:59

1 Answers1

2

You are sending the parameter as a GET parameter. You should get it from PhP from the $_GET

For example:

JavaScript:

xmlhttp.open("GET","webservices/getHotelDetails.php?q="+str,true);
xmlhttp.send();

PhP:

<?php
$q = $_GET['q'];

If you want to use POST instead, it is a bit more complicated. You can find the example here: Send POST data using XMLHttpRequest

Community
  • 1
  • 1
Jeroen de Lau
  • 640
  • 4
  • 13