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

PHP 实例 - AJAX 投票

<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">
                  <h2>AJAX 投票</h2>
<p>在下面的实例中&#xff0c;我们将演示一个投票程序&#xff0c;通过它&#xff0c;投票结果在网页不进行刷新的情况下被显示。</p>
<h3>你喜欢 PHP 和 AJAX 吗?</h3>
<p>是: <br> 否: </p>
<hr>
<h2>实例解释 - HTML 页面</h2>
<p>当用户选择上面的某个选项时&#xff0c;会执行名为 &#34;getVote()&#34; 的函数。该函数由 &#34;onclick&#34; 事件触发。</p>
<p><strong>poll.html</strong> 文件代码如下&#xff1a;</p>
<pre><html>
<head>
<meta charset&#61;&#34;utf-8&#34;>
<title>菜鸟教程(runoob.com)</title>
<script>
function getVote(int) {
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;poll&#34;).innerHTML&#61;xmlhttp.responseText;
    }
}
xmlhttp.open(&#34;GET&#34;,&#34;poll_vote.php?vote&#61;&#34;&#43;int,true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id&#61;&#34;poll&#34;>
<h3>你喜欢 PHP 和 AJAX 吗?</h3>
<form>
是:
<input type&#61;&#34;radio&#34; name&#61;&#34;vote&#34; value&#61;&#34;0&#34; οnclick&#61;&#34;getVote(this.value)&#34;>
<br>否:
<input type&#61;&#34;radio&#34; name&#61;&#34;vote&#34; value&#61;&#34;1&#34; οnclick&#61;&#34;getVote(this.value)&#34;>
</form>
</div>

</body>
</html></pre>
<p>getVote() 函数会执行以下步骤&#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;<strong>poll_vote.php</strong>&#34; 的 PHP 文件&#xff1a;</p>
<pre><?php
$vote &#61; htmlspecialchars($_REQUEST['vote']);

// 获取文件中存储的数据
$filename &#61; &#34;poll_result.txt&#34;;
$content &#61; file($filename);

// 将数据分割到数组中
$array &#61; explode(&#34;||&#34;, $content);
$yes &#61; $array;
$no &#61; $array;

if ($vote &#61;&#61; 0)
{
$yes &#61; $yes &#43; 1;
}

if ($vote &#61;&#61; 1)
{
$no &#61; $no &#43; 1;
}

// 插入投票数据
$insertvote &#61; $yes.&#34;||&#34;.$no;
$fp &#61; fopen($filename,&#34;w&#34;);
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>结果:</h2>
<table>
<tr>
<td>是:</td>
<td>
<span style&#61;&#34;display: inline-block; background-color:green;
      width:<?php echo(100*round($yes/($no&#43;$yes),2)); ?>px;
      height:20px;&#34; ></span>
<?php echo(100*round($yes/($no&#43;$yes),2)); ?>%
</td>
</tr>
<tr>
<td>否:</td>
<td>
<span style&#61;&#34;display: inline-block; background-color:red;
      width:<?php echo(100*round($no/($no&#43;$yes),2)); ?>px;
      height:20px;&#34;></span>
<?php echo(100*round($no/($no&#43;$yes),2)); ?>%
</td>
</tr>
</table></pre>
<p>当所选的值从 JavaScript 发送到 PHP 文件时&#xff0c;将发生&#xff1a;</p>
<ol><li>获取 &#34;poll_result.txt&#34; 文件的内容</li><li>把文件内容放入变量&#xff0c;并向被选变量累加 1</li><li>把结果写入 &#34;poll_result.txt&#34; 文件</li><li>输出图形化的投票结果</li></ol>
<hr>
<h2>文本文件</h2>
<p>文本文件&#xff08;<strong>poll_result.txt</strong>&#xff09;中存储来自投票程序的数据。</p>
<p>它存储的数据如下所示&#xff1a;</p>
<pre>3||4</pre>
<p>第一个数字表示 &#34;Yes&#34; 的投票数&#xff0c;第二个数字表示 &#34;No&#34; 的投票数。</p>
<p><strong>注释&#xff1a;</strong>请记得只允许您的 Web 服务器来编辑该文本文件。<strong>不要</strong>让其他人获得访问权&#xff0c;除了 Web 服务器 (PHP)。</p>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: PHP 实例 - AJAX 投票