admin 发表于 2023-2-16 18:54:45

PHP - AJAX 与 PHP

<div id="article_content" class="article_content clearfix">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
                <div id="content_views" class="htmledit_views">
                  <p>AJAX 被用于创建交互性更强的应用程序。</p>
<hr>
<h2>AJAX PHP 实例</h2>
<p>下面的实例将演示当用户在输入框中键入字符时&#xff0c;网页如何与 Web 服务器进行通信&#xff1a;</p>
<h2>实例</h2>
<p><strong>尝试在输入框中输入一个名字&#xff0c;如&#xff1a;Anna:</strong></p>
<p>姓名: </p>
<p>返回值:</p>
<p></p>
<p></p>
<hr>
<h2>实例解释 - HTML 页面</h2>
<p>当用户在上面的输入框中键入字符时&#xff0c;会执行 &#34;showHint()&#34; 函数。该函数由 &#34;onkeyup&#34; 事件触发&#xff1a;</p>
<pre><html>
<head>
<script>
function showHint(str)
{
    if (str.length&#61;&#61;0)
    {
        document.getElementById(&#34;txtHint&#34;).innerHTML&#61;&#34;&#34;;
        return;
    }
    if (window.XMLHttpRequest)
    {
        // IE7&#43;, Firefox, Chrome, Opera, Safari 浏览器执行的代码
        xmlhttp&#61;new XMLHttpRequest();
    }
    else
    {    
        //IE6, IE5 浏览器执行的代码
        xmlhttp&#61;new ActiveXObject(&#34;Microsoft.XMLHTTP&#34;);
    }
    xmlhttp.onreadystatechange&#61;function()
    {
        if (xmlhttp.readyState&#61;&#61;4 && xmlhttp.status&#61;&#61;200)
        {
            document.getElementById(&#34;txtHint&#34;).innerHTML&#61;xmlhttp.responseText;
        }
    }
    xmlhttp.open(&#34;GET&#34;,&#34;gethint.php?q&#61;&#34;&#43;str,true);
    xmlhttp.send();
}
</script>
</head>
<body>

<p><b>在输入框中输入一个姓名:</b></p>
<form>
姓名: <input type&#61;&#34;text&#34; οnkeyup&#61;&#34;showHint(this.value)&#34;>
</form>
<p>返回值: <span id&#61;&#34;txtHint&#34;></span></p>

</body>
</html></pre>
<p>源代码解释&#xff1a;</p>
<p>如果输入框是空的&#xff08;str.length&#61;&#61;0&#xff09;&#xff0c;该函数会清空 txtHint 占位符的内容&#xff0c;并退出该函数。</p>
<p>如果输入框不是空的&#xff0c;那么 showHint() 会执行以下步骤&#xff1a;</p>
<ul><li>创建 XMLHttpRequest 对象</li><li>创建在服务器响应就绪时执行的函数</li><li>向服务器上的文件发送请求</li><li>请注意添加到 URL 末端的参数&#xff08;q&#xff09;&#xff08;包含输入框的内容&#xff09;</li></ul>
<hr>
<h2>PHP 文件</h2>
<p>上面这段通过 JavaScript 调用的服务器页面是名为 &#34;gethint.php&#34; 的 PHP 文件。</p>
<p>&#34;gethint.php&#34; 中的源代码会检查姓名数组&#xff0c;然后向浏览器返回对应的姓名&#xff1a;</p>
<pre><?php
// 将姓名填充到数组中
$a[]&#61;&#34;Anna&#34;;
$a[]&#61;&#34;Brittany&#34;;
$a[]&#61;&#34;Cinderella&#34;;
$a[]&#61;&#34;Diana&#34;;
$a[]&#61;&#34;Eva&#34;;
$a[]&#61;&#34;Fiona&#34;;
$a[]&#61;&#34;Gunda&#34;;
$a[]&#61;&#34;Hege&#34;;
$a[]&#61;&#34;Inga&#34;;
$a[]&#61;&#34;Johanna&#34;;
$a[]&#61;&#34;Kitty&#34;;
$a[]&#61;&#34;Linda&#34;;
$a[]&#61;&#34;Nina&#34;;
$a[]&#61;&#34;Ophelia&#34;;
$a[]&#61;&#34;Petunia&#34;;
$a[]&#61;&#34;Amanda&#34;;
$a[]&#61;&#34;Raquel&#34;;
$a[]&#61;&#34;Cindy&#34;;
$a[]&#61;&#34;Doris&#34;;
$a[]&#61;&#34;Eve&#34;;
$a[]&#61;&#34;Evita&#34;;
$a[]&#61;&#34;Sunniva&#34;;
$a[]&#61;&#34;Tove&#34;;
$a[]&#61;&#34;Unni&#34;;
$a[]&#61;&#34;Violet&#34;;
$a[]&#61;&#34;Liza&#34;;
$a[]&#61;&#34;Elizabeth&#34;;
$a[]&#61;&#34;Ellen&#34;;
$a[]&#61;&#34;Wenche&#34;;
$a[]&#61;&#34;Vicky&#34;;

//从请求URL地址中获取 q 参数
$q&#61;$_GET[&#34;q&#34;];

//查找是否由匹配值&#xff0c; 如果 q>0
if (strlen($q) > 0)
{
    $hint&#61;&#34;&#34;;
    for($i&#61;0; $i<count($a); $i&#43;&#43;)
    {
        if (strtolower($q)&#61;&#61;strtolower(substr($a[$i],0,strlen($q))))
        {
            if ($hint&#61;&#61;&#34;&#34;)
            {
                $hint&#61;$a[$i];
            }
            else
            {
                $hint&#61;$hint.&#34; , &#34;.$a[$i];
            }
        }
    }
}

// 如果没有匹配值设置输出为 &#34;no suggestion&#34;
if ($hint &#61;&#61; &#34;&#34;)
{
    $response&#61;&#34;no suggestion&#34;;
}
else
{
    $response&#61;$hint;
}

//输出返回值
echo $response;
?></pre>
<p>解释&#xff1a;如果 JavaScript 发送了任何文本&#xff08;即 strlen($q) > 0&#xff09;&#xff0c;则会发生&#xff1a;</p>
<ol><li>查找匹配 JavaScript 发送的字符的姓名</li><li>如果未找到匹配&#xff0c;则将响应字符串设置为 &#34;no suggestion&#34;</li><li>如果找到一个或多个匹配姓名&#xff0c;则用所有姓名设置响应字符串</li><li>把响应发送到 &#34;txtHint&#34; 占位符</li></ol>
<h3>PHP Ajax 跨域问题解决方案</h3>
<p>如果你的异步请求需要跨域可以查看&#xff1a;<ahref="https://www.runoob.com/w3cnote/php-ajax-cross-border.html">PHP Ajax 跨域问题解决方案</a>。</p>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: PHP - AJAX 与 PHP