轻松上手,快乐学习!

PHP 教程

PHP 教程PHP 简介PHP 下载安装PHP 语法PHP 注释PHP 变量PHP Echo / PrintPHP 数据类型PHP 字符串PHP 数值PHP MathPHP 常量PHP 运算符PHP If...ElsePHP SwitchPHP 循环While 循环Do While 循环For 循环Foreach 循环Break/ContinuePHP 函数PHP 数组索引数组关联数组多维数组数组排序PHP 全局变量$GLOBALS$_SERVER$_REQUEST$_POST$_GETPHP 正则表达式PHP 表单处理PHP 表单验证PHP 表单验证必填字段PHP 表单验证URL/E-mailPHP 表单验证实例PHP 日期和时间PHP IncludePHP 文件处理PHP 文件打开/读取PHP 文件创建/写入PHP 文件上传PHP CookiesPHP SessionsPHP FiltersPHP Filters AdvancedPHP 回调函数PHP JSONPHP 异常处理PHP 什么是 OOPPHP 类和对象PHP 构造函数PHP 析构函数PHP 访问修饰符PHP 继承PHP 类常量PHP 抽象类PHP 接口PHP 特征PHP 静态方法PHP 静态属性PHP 命名空间PHP 可迭代对象MySQL DatabaseMySQL ConnectMySQL Create DBMySQL Create TableMySQL Insert DataMySQL Get Last IDMySQL Insert MultipleMySQL PreparedMySQL Select DataMySQL WhereMySQL Order ByMySQL Delete DataMySQL Update DataMySQL Limit DataPHP XML ParsersPHP SimpleXML ParserPHP SimpleXML - GetPHP XML ExpatPHP XML DOMAJAX IntroAJAX PHPAJAX DatabaseAJAX XMLAJAX Live SearchAJAX PollPHP 实例PHP 编译器PHP 测验PHP 练习

PHP 参考手册

PHP 概述PHP ArrayPHP CalendarPHP DatePHP DirectoryPHP ErrorPHP ExceptionPHP FilesystemPHP FilterPHP FTPPHP JSONPHP KeywordsPHP LibxmlPHP MailPHP MathPHP MiscPHP MySQLiPHP NetworkPHP Output ControlPHP RegExPHP SimpleXMLPHP StreamPHP StringPHP Variable HandlingPHP XML ParserPHP ZipPHP Timezones


PHP – AJAX 与 PHP


AJAX 被用于创建交互性更强的应用程序。


AJAX PHP 实例

下面的实例将演示当用户在输入框中键入字符时,网页如何与 Web 服务器进行通信:

实例

尝试在输入框中输入一个名字:

建议:


实例说明

当用户在上面的输入框中键入字符时,会执行 "showHint()" 函数。该函数由 "onkeyup" 事件触发:

以下是 HTML 代码:

实例

<html>
<head>
<script>
function showHint(str) {
  if (str.length == 0) {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("txtHint").innerHTML = this.responseText;
      }
    };
    xmlhttp.open("GET", "gethint.php?q=" + str, true);
    xmlhttp.send();
  }
}
</script>
</head>
<body>

<p><b>Start typing a name in the input field below:</b></p>
<form action="">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
运行实例 »

源代码解析:

首先,检查输入字段是否为空(str.length==0)。如果是,请清除 txtHint 占位符的内容并退出该函数。

如果输入框不是空的,那么 showHint() 会执行以下步骤:

  • 创建 XMLHttpRequest 对象
  • 创建在服务器响应就绪时执行的函数
  • 向服务器上的文件发送请求
  • 请注意添加到 URL 末端的参数(gethint.php?q="+str)(包含输入框的内容)
  • str 变量保存输入字段的内容

PHP 文件 - "gethint.php"

PHP 文件检查名称数组,并将相应的名称返回到浏览器:

<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";

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

$hint = "";

// lookup all hints from array if $q is different from ""
if ($q !== "") {
  $q = strtolower($q);
  $len=strlen($q);
  foreach($a as $name) {
    if (stristr($q, substr($name, 0, $len))) {
      if ($hint === "") {
        $hint = $name;
      } else {
        $hint .= ", $name";
      }
    }
  }
}

// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>