douxiongzhen2126 2016-04-13 06:30
浏览 67
已采纳

PHP警告:stristr():排空针

I get a strange behavior from the following code ..it's intended to give auto-complete options for the user on every key-press

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

when entering text in the input element the form unexpectedly creates another input element under the existed one and display multiple warning messages ... I tried to hide those warning messages using error_reporting(E_ERROR | E_PARSE); .. but the browser got very slow after I did that .... also I wanna know why another input element is displayed enter image description here

some help ? (the auto-complete options were shown successfully)

  • 写回答

1条回答 默认 最新

  • doqpm82240 2016-04-13 06:38
    关注

    It looks like the problem might be here:

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

    After the first iteration of the loop, you set $ACText to a blank string. Next time the loop runs, substr("$name", 0, 0) is going to return an empty string. It's the needle to stristr and, like the error message says, is empty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作