亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP ?? ?? ???? AJAX ??

AJAX ??

?? ???? ? ???? ?? ??? ?? ?? ??? ???? ?? ????? ?????.

???? ???? ?????.

? ?? ? ??? ???? ???? "getVote()"?? ??? ?????. ? ??? "onclick" ???? ?? ??????.

? ?? ? ???? ?????

  • HTML ??

  • PHP ??

  • TXT ??


HTML ??:

1.php ?? ?? ??

<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
<script>
function getVote(int) {
  //創(chuàng)建 XMLHttpRequest 對象
  if (window.XMLHttpRequest) {
    // IE7+, Firefox, Chrome, Opera, Safari 執(zhí)行代碼
    xmlhttp=new XMLHttpRequest();
  } else {
    // IE6, IE5 執(zhí)行代碼
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  //創(chuàng)建在服務(wù)器響應(yīng)就緒時執(zhí)行的函數(shù)
  xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("poll").innerHTML=xmlhttp.responseText;
    }
  }
  //向服務(wù)器上的文件發(fā)送請求
  xmlhttp.open("GET","2.php?vote="+int,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<div id="poll">
<h3>你喜歡 PHP 和 AJAX 嗎?</h3>
<!-- 用戶選擇一個選項,觸發(fā)onclick事件,執(zhí)行g(shù)etVote()函數(shù) -->
<form>
是:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)">
<br>否:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)">
</form>
</div>

</body>
</html>

? HTML ????? <div> ??? ? ?? ??? ??? ?? ??? HTML ??? ???? ????.

??? ??? ?? ?????.

  • ???? "?" ?? "???"? ???? ???? ??????.

  • ???? ????? getVote() ??? ?????.

  • ?? ???? "poll"??? <div> getVote() ???? ???? ???? ??? ???? ??? ?????.

getVote() ??? ?? ??? ?????.

  • XMLHttpRequest ?? ??

  • ??? in ?? ??? ????? ? ???? ??

  • ??? ?? ??? ?? ???

  • ??? ????(q)? ????? URL ???(???? ??? ?? ??)


PHP ??:

? ???? JavaScript? ??? ?? ???? "2.php"?? PHP ?????.

<?php
//過濾瀏覽器傳過來的數(shù)據(jù)
$vote = htmlspecialchars($_REQUEST['vote']);

// 獲取文件中存儲的數(shù)據(jù)
$filename = "3.txt";
$content = file($filename);

// 將數(shù)據(jù)分割到數(shù)組中
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
{
  $yes = $yes + 1;
}

if ($vote == 1)
{
  $no = $no + 1;
}

// 插入投票數(shù)據(jù)
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");//寫入方式打開
fputs($fp,$insertvote);//將$insertvote寫入文件中
fclose($fp);//關(guān)閉打開文件
?>

<h2>結(jié)果:</h2>
<table>
  <tr>
  <td>是:</td>
  <td>
  <span style="display: inline-block; background-color:green;
      width:<?php echo(100*round($yes/($no+$yes),2)); ?>px;
      height:20px;" ></span>
  <?php echo(100*round($yes/($no+$yes),2)); ?>%
  </td>
  </tr>
  <tr>
  <td>否:</td>
  <td>
  <span style="display: inline-block; background-color:red;
      width:<?php echo(100*round($no/($no+$yes),2)); ?>px;
      height:20px;"></span>
  <?php echo(100*round($no/($no+$yes),2)); ?>%
  </td>
  </tr>
</table>

??? ?? JavaScript?? PHP ??? ???? ??? ????

  • Get" poll_result.txt" ?? ??

  • ?? ??? ??? ?? ??? ??? 1? ????

  • ?? "poll_result.txt" ?? ??

  • ??? ?? ?? ??


TXT ??

??? ??(3.txt)? ?? ????? ???? ?????.

??? ????:

0||0

? ?? ??? '??' ??? ???? ? ?? ??? '??' ??? ?????.

??: ? ????? ? ??? ??? ??? ? ??? ???? ???. ? ??(PHP) ??? ?? ??? ?????? ???? ???.




????

? ???? ?? ?? ?? ???? ???? ????.

  • ?? ?? ??: ??? ??

  • onclick ???: ??? ??? ? ??

  • ?? ??, ?? ? ??

  • AJAX XMLHttpRequest ?? ??, ??? ??? ? ???? ??, ??? ??? ?? ???: ?? ?? ??? 1-5

  • HTML DOM getElementById() ???: ??? ID? ?? ? ?? ??? ?? ??? ?????.

PHP ?? ??? ?? ??:

  • file(): ?? ??? ??? ????.

  • fopen(): ?? ?? URL? ???

  • fputs(): ??? ??

  • fclose(): ?? ?? ?? ??

?? ??:

  • htmlspecialchars(): ?? ??? ??? HTML ???? ??

  • ??(): ??? ?? ??? ??

???? ??
||
<html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script> function getVote(int) { //創(chuàng)建 XMLHttpRequest 對象 if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 執(zhí)行代碼 xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 執(zhí)行代碼 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } //創(chuàng)建在服務(wù)器響應(yīng)就緒時執(zhí)行的函數(shù) xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("poll").innerHTML=xmlhttp.responseText; } } //向服務(wù)器上的文件發(fā)送請求 xmlhttp.open("GET","2.php?vote="+int,true); xmlhttp.send(); } </script> </head> <body> <div id="poll"> <h3>你喜歡 PHP 和 AJAX 嗎?</h3> <!-- 用戶選擇一個選項,觸發(fā)onclick事件,執(zhí)行g(shù)etVote()函數(shù) --> <form> 是: <input type="radio" name="vote" value="0" onclick="getVote(this.value)"> <br>否: <input type="radio" name="vote" value="1" onclick="getVote(this.value)"> </form> </div> </body> </html>