PHP警告:stristr():排空针

时间:2016-04-13 06:30:33

标签: javascript php jquery ajax

我从下面的代码中得到一个奇怪的行为..它打算在每次按键时为用户提供自动完成选项

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
function autoComplete(str) {
    debugger;
    if (str) {
        $.ajax({
            type:'GET' ,
            url:"index.php?q=" + str ,
            success:function(response){
                $("#autoComplete").html(response) ;
            }
        })
    }
};

$(document).ready(function() {
    $("#text").keypress(function() {
        autoComplete($("#text").val())
    });
})
</script>
</head>
<body>
<form action="index.php"  >
<input type="text" id="text"  />
<div id="autoComplete"></div>

</form>

<?php

if (isset($_GET["q"]))
{
  $a[] = "Android";
   $a[] = "B programming language";
   $a[] = "C programming language";
   $a[] = "D programming language";
   $a[] = "euphoria";
   $a[] = "F#";
   $a[] = "GWT";
   $a[] = "HTML5";
   $a[] = "ibatis";
   $a[] = "Java";
   $a[] = "K programming language";
   $a[] = "Lisp";
   $a[] = "Microsoft technologies";
   $a[] = "Networking";
   $a[] = "Open Source";
   $a[] = "Prototype";
   $a[] = "QC";
   $a[] = "Restful web services";
   $a[] = "Scrum";
   $a[] = "Testing";
   $a[] = "UML";
   $a[] = "VB Script";
   $a[] = "Web Technologies";
   $a[] = "Xerox Technology";
   $a[] = "YQL";
   $a[] = "ZOPL";
   $ACText = $_GET["q"] ; 
   $ACBox = "";

  foreach($a as $name)
  {
    if($name){
    if (stristr($ACText , substr("$name" , 0 , strlen($ACText))))
    {
        $ACText = $name ; 
        $ACBox .="<p>$ACText</p>" ; 
        $ACText = "" ; 
    }
  }
  }
  if($ACBox)
  {
    echo $ACBox  ; 
  }
    }

?>
</body>
</html>

在输入元素中输入文本时,表单意外地在现有文本下创建另一个输入元素并显示多条警告消息...我试图使用error_reporting(E_ERROR | E_PARSE)隐藏这些警告消息; ..但是我做完之后浏览器变得很慢....我也想知道为什么会显示另一个输入元素 enter image description here

有些帮助吗? (自动完成选项已成功显示)

1 个答案:

答案 0 :(得分:1)

看起来问题可能就在这里:

  foreach($a as $name)
  {
    if($name){
    if (stristr($ACText , substr("$name" , 0 , strlen($ACText))))
    {
        $ACText = $name ; 
        $ACBox .="<p>$ACText</p>" ; 
        $ACText = "" ; 
    }
  }
  }

在循环的第一次迭代之后,将$ACText设置为空字符串。下次循环运行时,substr("$name", 0, 0)将返回一个空字符串。它是针对stristr的指针,并且与错误消息一样,是空的。