编程语言
首页 > 编程语言> > JS AJAX --- PHP和ASP实例

JS AJAX --- PHP和ASP实例

作者:互联网

AJAX 用于创建更具交互性的应用程序

PHP 实例

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

不过,如果输入字段不为空,则进行如下:

<!DOCTYPE html>
<html>
<body>

<h1>XMLHttpRequest 对象</h1>

<h2>请在下面的输入字段中键入字母 A-Z:</h2>
<form>姓名:<input type="text" id="txt1" onkeyup="showHint(this.value)"></form>
<script>
function showHint(str) {
	var xhttp;
	if(str.length == 0){
	 document.getElementById("txtHint").innerHTML = "";
    return;
	}
	xhttp = new XMLHttpRequest();
	xhttp.onreadystatechange = function() {
	if(this.readyState == 4 && this.status == 200){
	 document.getElementById("txtHint").innerHTML = this.responseText;
	};
	  xhttp.open("GET", "/demo/gethint.php?q="+str, true);
  xhttp.send(); 
	}
}
</script>
</body>
</html>

PHP 文件 - “gethint.php”

这个 PHP 文件检查姓名数组,然后向浏览器返回对应的姓名:

<?php
// 姓名数组
 $a[] = "Ava";
 $a[] = "Brielle";
 $a[] = "Caroline";
 $a[] = "Diana";
 $a[] = "Elise";
 $a[] = "Fiona";
 $a[] = "Grace";
 $a[] = "Hannah";
 $a[] = "Ileana";
 $a[] = "Jane";
 $a[] = "Kathryn";
 $a[] = "Laura";
 $a[] = "Millie";
 $a[] = "Nancy";
 $a[] = "Opal";
 $a[] = "Petty";
 $a[] = "Queenie";
 $a[] = "Rose";
 $a[] = "Shirley";
 $a[] = "Tiffany";
 $a[] = "Ursula";
 $a[] = "Victoria";
 $a[] = "Wendy";
 $a[] = "Xenia";
 $a[] = "Yvette";
 $a[] = "Zoe";
 $a[] = "Angell";
 $a[] = "Adele";
 $a[] = "Beatty";
 $a[] = "Carlton";
 $a[] = "Elisabeth";
 $a[] = "Violet";

// 从 URL 获取 q 参数
$q = $_REQUEST["q"];

$hint = "";

// 查看数组中所有 hint,$q 是否与 "" 相同
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";
            }
         }
    }
}

// 输出 "no suggestion",如果未找到 hint 或输出正确的值
  echo $hint === "" ? "no suggestion" : $hint;
?>

ASP 文件 - “gethint.asp”

这个 ASP 文件检查姓名数组,然后向浏览器返回对应的姓名:

<%
 response.expires=-1
 dim a(32)
 '用姓名填充数组
 a(1)="Ava"
 a(2)="Brielle"
 a(3)="Caroline"
 a(4)="Diana"
 a(5)="Elise"
 a(6)="Fiona"
 a(7)="Grace"
 a(8)="Hannah"
 a(9)="Ileana"
 a(10)="Jane"
 a(11)="Kathryn"
 a(12)="Laura"
 a(13)="Millie"
 a(14)="Nancy"
 a(15)="Opal"
 a(16)="Petty"
 a(17)="Queenie"
 a(18)="Rose"
 a(19)="Shirley"
 a(20)="Tiffany"
 a(21)="Ursula"
 a(22)="Victoria"
 a(23)="Wendy"
 a(24)="Xenia"
 a(25)="Yvette"
 a(26)="Zoe"
 a(27)="Angell"
 a(28)="Adele"
 a(29)="Beatty"
 a(30)="Carlton"
 a(31)="Elisabeth"
 a(32)="Violet"

 '从 URL 获取 q 参数
 q=ucase(request.querystring("q"))

 '查看数组中所有 hint,q 的长度是否大于 0
 if len(q)>0 then
   hint=""
   for i=1 to 30
     if q=ucase(mid(a(i),1,len(q))) then
       if hint="" then
         hint=a(i)
       else
         hint=hint & " , " & a(i)
       end if
     end if
   next
 end if

 '如果未找到 hint,输出 "no suggestion",或输出正确的值
 if hint="" then
   response.write("no suggestion")
 else
   response.write(hint)
 end if
%>

标签:gethint,ASP,name,hint,xhttp,JS,---,str,PHP
来源: https://blog.csdn.net/m0_53170889/article/details/119452628