-2
*,當我選擇yes或no後,寬度立即出現。我想要的是動畫寬度。我用動畫搜索了另一個例子,但我仍然無法在這個例子中應用動畫。如何在w3schools的這個輪詢示例中動畫顯示w3schools中的ajax poll的寬度
達到此目的的最佳方法是什麼? *
<!DOCTYPE HTML>
<html>
<head>
<script>
function getVote(int) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("poll").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body><div id="poll">
<h3>Do you like PHP and AJAX so far?</h3>
<form>
Yes:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)">
<br>No:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)">
</form>
</div>
</body>
</html>
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0) {
$yes = $yes + 1;
}
if ($vote == 1) {
$no = $no + 1;
}
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<div style="background-color: blue; width:<?php echo(100*round($yes/($no+$yes),2)); ?>px; height:20px ">
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</div>
</td>
</tr>
<tr>
<td>No:</td>
<td>
<div style="background-color: blue; width:<?php echo(100*round($no/($no+$yes),2)); ?>px; height:20px ">
<?php echo(100*round($no/($no+$yes),2)); ?>%
</div>
</td>
</tr>
</table>
月1日:您遺漏了DIV '民意調查' 結束標記:''
第二 – Jeff:'int'是JS的保留字,因此重新命名'int'到像'value'或'投票' – Jeff
第3次:你錯過了關閉';'在高度後的樣式定義。這不應該犯錯誤,但仍然更好地學習編碼! - 在<<?之後相同php回聲.....; ?> – Jeff