-
[PHP] 55. AJAX Poll - PHP 강좌, PHP5Web/PHP 2015. 6. 13. 14:16
PHP Example - AJAX Poll
1. AJAX Poll
아래 예제는 리로드 없이 투표 결과를 보이는 예제입니다.
2. Example Explained - The HTML Page
12345678910111213141516171819202122232425262728293031323334<html><head><script>function getVote(int) {if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest();} else { // code for IE6, IE5xmlhttp=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>cs 3. The PHP File
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<?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><img src="poll.gif"width='<?php echo(100*round($yes/($no+$yes),2)); ?>'height='20'><?php echo(100*round($yes/($no+$yes),2));?>%</td></tr><tr><td>No:</td><td><img src="poll.gif"width='<?php echo(100*round($no/($no+$yes),2)); ?>'height='20'><?php echo(100*round($no/($no+$yes),2));?>%</td></tr></table>cs * 이 강좌는 'w3schools'를 참조하여 작성하였습니다.
'Web > PHP' 카테고리의 다른 글
[PHP] 54. AJAX RSS Reader - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 53. AJAX 실시간 검색 ( AJAX Live Search) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 52. AJAX 와 XML ( AJAX and XML) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 51. AJAX 와 MySQL ( AJAX and MySQL) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 50. AJAX 와 PHP(AJAX and PHP) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 49. AJAX 소개( AJAX Introduction) - PHP 강좌, PHP5 (0) 2015.06.13