如何从javascript调用php文件?

如何从javascript调用php文件?,javascript,php,html,xmlhttprequest,Javascript,Php,Html,Xmlhttprequest,我有两个文件SearchHotelUsingXMLhttp.html,另一个文件是getHotelDetails.php,它位于webservices目录中。 现在,对于给定的酒店搜索,我创建了一个按钮搜索,从SearchButton调用java脚本函数searchHotel(str)。通过这个JavaScript函数,我使用xmlHttp请求调用getHotelDetails.php,但我得到以下消息 注意:第2行的C:\xampp\htdocs\Experiements\webservice

我有两个文件SearchHotelUsingXMLhttp.html,另一个文件是getHotelDetails.php,它位于webservices目录中。 现在,对于给定的酒店搜索,我创建了一个按钮搜索,从SearchButton调用java脚本函数searchHotel(str)。通过这个JavaScript函数,我使用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>

功能酒店(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”,“webservices/getHotelDetails.php?q=“+str,true”);
xmlhttp.send();
}
} 
旅馆
城市:
班加罗尔
钦奈



酒店信息将列在这里。
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);
?> 


您正在将参数作为GET参数发送。您应该从PhP从$\u get获取它

例如:

JavaScript:

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


输出POST时给出了什么?另外,在if语句之外在哪里声明xmlhttp?尝试将第2行更改为:$q=$\u GET['q'];看起来您正在将其设置为post,但却发送GET参数。(您的脚本也容易受到SQL注入的攻击。)是的,请尝试将$\u POST更改为$\u GET以获取查询字符串params@BenisonSam我需要Post方法,那么我将如何做呢?您可以使用$\u GET访问查询字符串,使用$\u Post访问表单提交值。因此,请相应地使用它们。
<?php
$q = $_GET['q'];