-2
*この投票のw3schoolsの例では、幅は、yesまたはnoを選択した直後に表示されます。私はアニメーションで別の例を探しましたが、この例ではまだアニメーションを適用できません。w3schoolsからこのajaxポーリングの幅をアニメートする方法
これを達成する最も良い方法は何ですか? *
<!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>
第一:あなたはdivの「世論調査」の終了タグが欠落している: ''
– Jeff第二: 'int'はJSの予約語なので、[値]'のようなものに 'int'の名前を変更しますか、 'vote' – Jeff
3番目:高さの後にスタイル定義で'; 'を閉じることができません。それはエラーを起こしてはいけませんが、それでもコードを正確に書くことを学ぶ方がいいです! - '<? PHPエコー.....; ?> ' – Jeff