AJAX搜索没有返回任何内容

时间:2013-12-15 16:42:40

标签: javascript php jquery html ajax

我改变了你的两个建议。地址栏之前没有改变,所以我不确定目前发生了什么。当我点击立即搜索时,search_results div中没有​​任何内容,地址栏变为:

  ?

的index.php habbo_name =试验&安培;送=搜索

当前index.php:

<html>
<head>
  <title>Personnel Tracking System - E-3+</title>
  <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

  <script language="JavaScript">
    function tS(){ x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()); return x; }
    function tN(){ return new Date(); }
    function lZ(x){ return (x>9)?x:'0'+x; }
    function y2(x){ x=(x<500)?x+1900:x; return String(x).substring(2,4) }
    function dT(){ if(fr==0){ fr=1; document.write('<font size=2 face=Arial color=white><b><span id="tP">'+eval(oT)+'</span></b></font>'); } document.getElementById('tP').innerHTML=eval(oT); setTimeout('dT()',1000); }
    var mN=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),fr=0,oT="tS().getDate()+' '+mN[tS().getMonth()]+' '+y2(tS().getYear())+' '+':'+':'+' '+lZ(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+' '";
  </script>

  <script type="text/javascript">
    $(function() {
      $("#search_habbos").bind('submit',function() {
        $.post( "search_results.php", { value: $('#habbo_name').val() })
            .done(function( data ) {
              $("#search_results").html(data);
            });
        return false;
      });
    });
  </script>

</head>
<body>

<script>
  $(document).ready( function(){
    $.fn.snow();
  });
</script>

<div id="navbar">
  <img src="images/logo.png" style="position: absolute; top: 7px; left: 50px;" />
  <center>
    <img class="navbar" src="images/homebutton.png" />
    <img class="navbar" src="images/e-3button.png" />
    <img class="navbar" src="images/resignedbutton.png" />
    <img class="navbar" src="images/firedbutton.png" />
    <img class="navbar" src="images/desertersbutton.png" />
    <img class="navbar" src="images/mosrosterbutton.png" />
    <img src="images/divider.png" />
    <p><script language="JavaScript">dT();</script></p>
  </center>
</div>

<div id="search">
  <center>
    <b><p>Search</p></b>
    <form action="" id="search_habbos">
      <input type="text" name="habbo_name" id="habbo_name" size="30">
      <br>
      <input type="submit" value="Search" name="send" id="send">
    </form>
  </center>
  <div id="search_results"></div>
</div>

</html>

当前的search_results.php:

<?php
    global $con;
        $habbo_name_final = '%'.$_POST['value'].'%';

        if (empty($_POST) === false) {
            global $con;
            $query = "SELECT * FROM `personnel` WHERE `habbo_name` LIKE :search ORDER BY `habbo_name`";
            $result = $con->prepare($query);
            $result->bindParam(':search', $habbo_name_final, PDO::PARAM_STR);
            $result->execute();

            while($row = $result->fetch()) {
                echo "<b>" . htmlspecialchars($row['status']) . ":</b> &nbsp" . htmlspecialchars($row['habbo_name']) . "</b> &nbsp" . htmlspecialchars($row['rank']) . "&nbsp" . htmlspecialchars($row['rating']) . "&nbsp" . htmlspecialchars($row['tag']) . "&nbsp" . htmlspecialchars($row['asts']) . "&nbsp" . htmlspecialchars($row['promotion_date']) . "&nbsp <b>" . htmlspecialchars($row['rdc_grade']) . "</b> &nbsp" . htmlspecialchars($row['medals']) . htmlspecialchars($row['transfer_rank_received']) . "&nbsp" . htmlspecialchars($row['cnl_trainings']) . "&nbsp" . htmlspecialchars($row['fired_reason']) . htmlspecialchars($row['desert_date']) . htmlspecialchars($row['quit_date']) . htmlspecialchars($row['fired_tag']) . "&nbsp" . htmlspecialchars($row['fired_date']) . "<br />";
            }
        } else {
            echo 'Enter a search term!';
        }   
        if (empty($row) == false) {
            echo "No results found";
        }
?>

1 个答案:

答案 0 :(得分:1)

我理解你对此有何意义(mysql,变量中的plop等)。

PHP的第3行:

$habbo_name = '".$_POST['value']."'

首先,需要一个分号;

然后,将内部双引号移动到value左右。从本质上讲,这会给$habbo_name一个确切的".$_POST[值,然后它就会出错,因为你早期杀死了表达式,PHP看到了value并翻了出来。

修正:

$habbo_name_final = '%' . $_POST["value"] . '%';

这里不需要使用两个变量。一个人会做得很好。这类似于我下面的答案,但是他的解析也不会因为他像你在表达式上那样提前退出。

编辑:

你的整个页面看起来都是从1998年开始。要指出太多的东西,所以我们正在跳过它。

要进行问题排查,请将整个AJAX表达式移到页面末尾,然后尝试使用它而不是现在的内容:

$('#search_habbos').bind('submit', function() {
    $.post('search_results.php', { value: $('#habbo_name').val() }, function( data ) {
        $('#search_results').prepend( data );
    });
});

而且我非常确定您的SQL查询需要两个' ' s。

"SELECT * FROM `personnel` WHERE `habbo_name` LIKE :search ORDER BY `habbo_name`";

需要:

"SELECT * FROM `personnel` WHERE `habbo_name` LIKE ':search' ORDER BY `habbo_name`";

告诉我们这对你有用吗<​​/ p>