简体   繁体   English

ajax实时建议搜索不起作用

[英]ajax live suggestion search not working

I'm trying to create a live search, but the console is always giving me an error, could someone have a look at what I'm doing? 我正在尝试创建实时搜索,但是控制台始终会给我一个错误,有人可以看看我在做什么吗? thanks in advance 提前致谢

so this is my html code: 所以这是我的html代码:

<li id="ZoekBalk">
<!--De zoekbalk-->
    <form action='films.php' method='get'id='zoekform'>
        <input id="ZoekBalkSearch" type="search" name="zoekparameter" placeholder="Geef een zoekterm in." onkeyup="ajaxzoeken(this.value)" />
        <input id="ZoekButton" type="submit" value="Zoek"/>
    </form>
    <div id="resultatenPlaatshouder"></div>
</li>

My javascript function: 我的JavaScript函数:

function ajaxzoeken( str ) {
   if ( str.length == 0 ) { 
      document.getElementById("resultatenPlaatshouder").innerHTML = "";
      return;
  }
  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("resultatenPlaatshouder").innerHTML = xmlhttp.responseText;
      }
  }
  xmlhttp.open("GET", "ajaxzoeken.php?teZoeken = " + str, true);
  xmlhttp.send();
}

and my php-code: 和我的PHP代码:

<?php
   require 'data.php';
   if ( isset( $_GET['teZoeken'] ) ) {
      $tezoeken=$_GET['teZoeken'];
      //voeg eventuele extra karakters toe aan de zoekquery
      $tezoeken = '%'.$tezoeken.'%';

      //zoek in de database (gebruik ook bindparams als beveiliging tegen sql injection)
      $query = $connection->prepare("SELECT id,filmnaam,posterlink FROM films WHERE filmnaam ILIKE :zoek ORDER BY filmnaam;");

      $query->bindParam(':zoek',$tezoeken,PDO::PARAM_STR);
      $query->execute();

      //geef alle gevonden films terug in een table
      echo '<table>'

      while ( $row = $query->fetch( PDO::FETCH_BOTH ) ) {
          $id = $row[0];
          $filmnaam = $row[1];
          $poster = $row[2];

          echo "<tr><td><a href='moviePage.php?id=$id'><img class='miniposter' src='$poster' alt='De poster van de film'/></a></td><td><a href='moviePage.php?id=$id'><strong>$filmnaam</strong></a></td></tr>";
      }
      echo '</table>';
   }
?>

I found it, I forgot the ; 我找到了,我忘记了; in the echoes, damn what a stupid mistake 在回声中,该死的是一个愚蠢的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM