Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 试图让第一个Ajax程序工作,没有任何动态变化_Php_Ajax_Wamp - Fatal编程技术网

Php 试图让第一个Ajax程序工作,没有任何动态变化

Php 试图让第一个Ajax程序工作,没有任何动态变化,php,ajax,wamp,Php,Ajax,Wamp,我基本上是在复制,当你开始输入一个名字时,会添加一些建议。没有为我添加任何建议 nameguess.html <!DOCTYPE html> <html lang="en-ca"> <head> <meta charset="utf-8" /> <title>Ajaxing</title> <script type="text/javascript"> <!-

我基本上是在复制,当你开始输入一个名字时,会添加一些建议。没有为我添加任何建议

nameguess.html

<!DOCTYPE html>
<html lang="en-ca">
<head>
    <meta charset="utf-8" />
    <title>Ajaxing</title>
        <script type="text/javascript">
        <!--
            function showHint(str)
            {
            var xmlhttp;
            if (str.length == 0)
            {
              document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","gethint.php?q="+str,true);
            xmlhttp.send();
            }
        //-->
        </script>
</head>
<body>
Hello.<br />
<form method="GET" action="">
    Name: <input type="text" id="txt1" /><br />
    Suggestions: <span id="txtHint"></span>
</body>
</html>

阿贾兴
你好。
名称:
建议:
gethint.php

<?php
// Fill up array with names
$a[]="Anna";
//removed code to save space
$a[]="Vicky";

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        $hint=$a[$i];
        }
      else
        {
        $hint=$hint." , ".$a[$i];
        }
      }
    }
  }

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;
?>

我的两个文件都位于WAMP的www文件夹中,并且虚拟服务器正在运行。

更改此设置

<input type="text" id="txt1" />



并查看这是否有帮助

您错过了按键事件绑定:

<input type="text" id="txt1" onkeyup="showHint(this.value)">

此外,您的PHP可以简化为

<?php

if(!isset($_GET['q']) exit;

// Fill up array with names
$names = array("Anna", "Vicky");

//get the q parameter from URL
$q=$_GET["q"];

$hints = array();
foreach($names as $name) {
   if (stripos($name, $q) !== -1) $hints[] = $name;
}

if(count($hints) == 0) die("no suggestion");

echo implode(', ', $hints);

?>

我可以看到其他人已经回答了您试图做的事情,但我建议不要像这样提出ajax请求

相反,尝试使用jQuery库发出ajax请求。我能够把上面的代码缩短成这样

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $.get(
        'gethint.php', //Code behind 

        {q : $("#txt1").val()}, //json string of variables to post

        function(data){$("#txtHint").html(data);}, // What to do with return data

        'json'); //Specify json 
    </script>

美元(
'gethint.php',//代码隐藏
{q:$(“#txt1”).val()},//要发布的变量的json字符串
函数(data){$(“#txtHint”).html(data);},//如何处理返回数据
“json”)//指定json
从长远来看,这将使生活变得更加轻松,因为必须经历整个混乱,并且必须担心跨浏览器的兼容性,这很糟糕


您可以在这里找到更多信息

“IE6、IE5的代码”-真的吗?为什么不使用类似的代码?我发现它更简单,跨浏览器友好。从w3白痴那里调用showHint(str)的代码是什么?请看看为什么你应该在其他地方找到一个教程@paul“IE6,IE5的代码”有什么问题?先“艰难地”学习它,不使用JQuery有什么好处吗?通常我一次只学一件事。在我个人看来,不是,而是各学各的。如果它适合你的学习风格,那就去做吧。我只是想让你比使用更复杂的方法更快地结束(进行成功的ajax查询)。服务器返回的是逗号分隔的名称列表,而不是json。onchange和onkeyup之间有区别吗?是的,
onchange
在你从输入中获取焦点时被激发,但是,
keyup
顾名思义,每次释放一个键时都会触发。顺便说一句,我建议@Romuloux使用类似jQuery的库来处理类似的内容。这只在用户单击另一个输入字段时起作用,我只有1个。
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $.get(
        'gethint.php', //Code behind 

        {q : $("#txt1").val()}, //json string of variables to post

        function(data){$("#txtHint").html(data);}, // What to do with return data

        'json'); //Specify json 
    </script>