Jump to content

Ajax Poll


Recommended Posts

Hello, I would like to know how to publish the Ajax poll http://www.w3schools...p_ajax_poll.asp in my website that works the same way it does in the demo page. The demo page shows the results in the same page without disturbing all the content around it. When I install this php in may page, it echoes the results in a blank page intead of appearing in the page and blending it with the rest of the content. HTML= <html><head><script type="text/javascript">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= <?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> Thanks. JLB

Edited by Badijoe
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...