javascript - PHP警告: stristr(): Empty needle in line

标签 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

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

最佳答案

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

  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 的针,并且如错误消息所示,是空的。

关于javascript - PHP警告: stristr(): Empty needle in line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36590331/

相关文章:

javascript - 如何根据用于提交的表单 ID 更改用户消息?

javascript - Durandal SPA 与 typescript 有关的问题

javascript - 如何在 <hr> 上制作小箭头按钮(用于 JQuery 的 slideUp/slideDown)

php - 这个 PHP MySQL 查询有什么问题?

php - 多文件上传,相册,mysql

php - 如何设置表单的 DB 代码

javascript - 无法更改js节点文本的颜色

javascript - 如何在 JavaScript 中重新格式化 JSON?

javascript - 带有ajax xml请求的 Node js脚本不起作用

javascript - 同一页面上有多个 Google 图表