在没有运行的情况下在另一个中包含一个php文件(不能使用函数)

时间:2015-10-27 05:16:34

标签: php ajax

我正在尝试为我的网站制作一个类似Google的搜索栏,它会自动更新搜索结果
我正在使用php和AJAX来做这件事,这是AJAX的一部分,所以我可以进一步解释 edited below
所以希望你们都知道AJAX,我在html表单中的一个事件属性中调用search()函数onkeyup
它正在运行AJAX,我用一点alert('hello!');来测试它 现在我的问题是我在以下代码中得到一个未定义的索引错误:

<?php
require 'connection.php';
require '../search.php';
$term = $_POST['searchword'];
$type = $_POST['searchtype'];
echo $term . $type;
  $term = mysqli_real_escape_string($con,$term);
  $type = mysqli_real_escape_string($con,$type);
  $query = "SELECT * FROM `users` WHERE `username` LIKE '$term%' AND `type` = '$type'";
  $result = mysqli_query($con,$query);
  while($row = mysqli_fetch_assoc($result)){
    $name = $row['username'];
    $ntype = $row['type'];
    $id = $row['user_id'];
    echo "<a href = \"user.php?id=$id\"><div class=\"col-lg-12 result\">$name</br>$ntype</div></a>";
  }if($rowcnt = mysqli_num_rows($result) == 0){
    echo"<div class=\"col-lg-12 result center\">No results found</div>";
  }

 ?>

未定义索引错误适用于$term$type,即$_POST['searchword'];$_POST['searchtype'];
我仔细检查了代码,并意识到search.php页面不包括在内......所以&#34; searchword&#34;和&#34; searchtype&#34;没有定义,所以我尝试包括它,现在每次我点击搜索栏中的按钮我得到第二个搜索表单,因为代码也运行search.php onkeyup,所以我想知道是否有任何方法可以避免这种情况从发生中 顺便说一下,我仍然得到未定义的索引错误,即使它被包括在内 这些是我正在使用的所有文件:
search.php:

<?php
session_start();
require 'res/connection.php';
include 'res/loggedinmenu.php';
if(empty($_SESSION['id'])){
  echo"<script>
  setTimeout(function () {
  window.location.href = 'index.php';},2000);
  </script>";
  echo "
  <div class=\"alert alert-info\">
    <strong>You are not logged in!</strong> in 2 seconds you will be redirected to the login page
  </div>
  ";
}
 ?>
 <!DOCTYPE>
 <html>
 <head>
   <title> search for other developers or owners</title>
   <link rel="stylesheet" type="text/css" href="css/profile.css">
   <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
   <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
   <style>
   body{
     background-color:#dcdcdc;
   }
   .all{
       margin-top:110px;
   }
   .search{
     border:1px solid #b0b0b0;
     background-color:#e4e4e4;
     display:inline;
     box-shadow: 2px 2px 3px #ccc;
     margin-bottom:35px;
   }
   .header{
     width:100%;
     background-color:#3928f2;
     color:white;
     text-align:center;
     padding-top:15px;
     padding-bottom:15px;
     font-weight:bold;
     font-size:20px;
   }
   .form{
     padding-top:20px;
     border-bottom:1px solid #ccc;
   }
   .result{
     font-size:20px;
     margin-top:15px;
     padding-bottom:5px;
     border-bottom:1px solid #ccc;
     text-decoration:none;
   }
   .result :hover{
     text-decoration:none;
   }
   .center{
     text-align:center;
   }
   </style>
 </head>
 <body>
   <?php menu(); ?>
   <div class="container all">
     <div class="row">
       <div class="col-lg-2">
       </div>
       <div class="col-lg-8 search">
         <div class="row">
         <div class="col-lg-12 header">search for a developer or an owner</div>
         <div class="col-lg-12 form">
         <form action="" method="post" style="text-align:center;">
           <div class="row">
            <div class="col-lg-6">
           <label for="term">Search term :</label>
           <input type="text" onkeyup="search()" class="form-control" name="searchword" id="searchbar" value = "<?php if (isset($_GET['term'])){ echo $_GET['term']; }?>"/>
         </div>
           <div class="col-lg-6">
           <label for="type">looking for a :</label>
           <select name="searchtype" class="form-control" id="stype">
             <option>owner</option>
             <option>developer</option>
           </select></br>
       </div>
     </div>
         </div>
         </form>
       <hr>
       </div>

       <div class="row">
          <div  id="results" class="col-lg-12">
            <?php
            if(isset($_GET['term']) && isset($_GET['type'])){
            $term = $_GET['term'];
            $type = $_GET['type'];
          }else{
            $term = $_POST['searchword'];
            $type = $_POST['searchtype'];
          }
            if($term == ""){
              echo '<div class="col-lg-12 result center">Type in the box above to start searching</div>';
            }else{
              $term = mysqli_real_escape_string($con,$term);
              $type = mysqli_real_escape_string($con,$type);
              $query = "SELECT * FROM `users` WHERE `username` LIKE '$term%' OR `username` LIKE '%$term' AND `type` = '$type'";
              $result = mysqli_query($con,$query);
              while($row = mysqli_fetch_assoc($result)){
                $name = $row['username'];
                $ntype = $row['type'];
                $id = $row['user_id'];
                echo "<a href = \"user.php?id=$id\"><div class=\"col-lg-12 result\">$name</br>$ntype</div></a>";
              }if($rowcnt = mysqli_num_rows($result) == 0){
                echo"<div class=\"col-lg-12 result center\">No results found</div>";
              }
            }
             ?>
          </div>
       </div>
       </div>
       </div>
       <div class="col-lg-2">
         <!--leave empty -->
       </div>
     </div>
   </div>
 </body>
 </html>
 <script>
 function search(){
   var xhttp = new XMLHttpRequest();
   var url = "res/searchsyntax.php";
   var trm = document.getElementById('searchbar').value;
   var tpe = document.getElementById('stype').value;
   var params = "term=" + term + "&type=" + tpe;
   http.open("POST", url, true);

   //Send the proper header information along with the request
   http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http.setRequestHeader("Content-length", params.length);
   http.setRequestHeader("Connection", "close");

   http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        document.getElementById("results").innerHTML = http.responseText;
    }
   }
   http.send(params);
 }
 </script>

res / searchsyntax.php:

    <?php
require 'connection.php';
$term = $_GET['term'];
$type = $_GET['type'];
if($term == ""){
  echo '<div class="col-lg-12 result center">Type in the box above to start searching</div>';
}else{
  $term = mysqli_real_escape_string($con,$term);
  $type = mysqli_real_escape_string($con,$type);
  $query = "SELECT * FROM `users` WHERE `username` LIKE '$term%' AND `type` = '$type'";
  $result = mysqli_query($con,$query);
  while($row = mysqli_fetch_assoc($result)){
    $name = $row['username'];
    $ntype = $row['type'];
    $id = $row['user_id'];
    echo "<a href = \"user.php?id=$id\"><div class=\"col-lg-12 result\">$name</br>$ntype</div></a>";
  }if($rowcnt = mysqli_num_rows($result) == 0){
    echo"<div class=\"col-lg-12 result center\">No results found</div>";
  }
}
 ?>

1 个答案:

答案 0 :(得分:1)

您收到此错误是因为您没有通过AJAX将帖子参数(即“searchword”和“searchtype”)发送到PHP代码。

This code演示了如何执行此操作。

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);