如何从JavaScript调用php文件?

时间:2014-12-24 12:43:12

标签: javascript php html xmlhttprequest

我有两个文件 SearchHotelUsingXMLhttp.html ,其他文件是 getHotelDetails.php ,它位于 webservices 这是一个目录。 现在对于给定的酒店搜索我创建了一个按钮搜索,从搜索按钮我调用java脚本函数searchHotel(str)。从这个javacript函数我使用xmlHttp请求调用getHotelDetails.php,但是我收到以下消息。

注意:第2行的C:\ xampp \ htdocs \ Experiements \ webservices \ getHotelDetails.php中的未定义索引:q
城市名称

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

1 个答案:

答案 0 :(得分:2)

您将参数作为GET参数发送。你应该从$ _GET

中获得PhP

例如:

JavaScript的:

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

PHP的:

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

如果你想使用POST,它会有点复杂。你可以在这里找到这个例子: Send POST data using XMLHttpRequest