Jump to content

nielse63

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

nielse63's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I have two PHP polls on one page right now using AJAX, and I'm having some trouble with the way that they're being displayed. As of right now, the results for each individual poll are showing up in the same place (the second polls original div tag). I would prefer it if the user to could vote on poll one, have the results displayed in the first div tag, then move on to the second poll and have the results displayed in the second div tag. Here's the code: HTML Code (for the initial display page) <html> <head> <script type="text/javascript" src="poll.js"></script> </head> <body> Vote <div id="poll1"> <form name="form1" id="form1"> Vote 1-Up <input type="radio" name="vote" value="0" onclick="getVote1(this.value)" /> Vote 1-Down <input type="radio" name="vote" value="1" onclick="getVote1(this.value)" /> </form> </div> <p> </p> Vote <div id="poll2"> <form name="form2" id="form2"> Vote 2-Up <input type="radio" name="vote" value="0" onclick="getVote2(this.value)" /> Vote 2-Down <input type="radio" name="vote" value="1" onclick="getVote2(this.value)" /> </form> </div> </body> </html> The JS Source Code: var xmlhttp; function getVote1(int) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="poll_vote_test1.php"; url=url+"?vote1="+int; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("poll1").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { var objXMLHttp=null; if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); } return objXMLHttp; } var xmlhttp; function getVote2(int) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="poll_vote_test2.php"; url=url+"?vote2="+int; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("poll2").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { var objXMLHttp=null; if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); } return objXMLHttp; } And finally, the PHP Code. There are two separate pages, as you can see from JS, but I'm only displaying one for the sake of space: <body> <?php $vote1 = $_REQUEST['vote1']; //get content of textfile $filename = "poll_result_test1.txt"; $content = file($filename); //put content in array $array = explode("||", $content[0]); $yes = $array[0]; $no = $array[1]; if ($vote1 == 0) { $yes = $yes + 1; } if ($vote1 == 1) { $no = $no + 1; } //insert votes to txt file $insertvote = $yes."||".$no; $fp = fopen($filename,"w"); fputs($fp,$insertvote); fclose($fp); ?> <strong>Result:</strong> <table id="result1"> <tr> <td>Yes:</td> <td> <img src="poll.jpg" 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.jpg" width='<?php echo(100*round($no/($no+$yes),2)); ?>' height='20'> <?php echo(100*round($no/($no+$yes),2)); ?>% </td> </table> </body> </html> This is really just a sample of what I'm trying to do, I guess, but the way that each are displaying is really becoming a headache. Any help would be appreciated. Thanks!
  2. How would I write the PHP script so that it can be recalled? I checked out counter tutorials and examples, and all of the ones that I wrote from there work great for recalling the number of hits, but I'm looking to collect data on the number of clicks and have that automatically posted. Any hints? Thanks.
  3. I'm having some trouble with a click counter that's on my website. Whenever a user clicks on "Good" or "Bad" the number next to it will go up by one, but the problem is that I can't figure out how to store the data for each click so that when the user returns the number remains at what it was (or is higher because other users have clicked on it). As of right now the number is reset to 0 every time the page is reloaded. Here's the code: <script> function add_to_goodcount1(){ var goodcount1=document.getElementById('goodcount1'); var cstore = document.getElementById('good_counter_score1'); cstore.value=parseInt(cstore.value)+1; goodcount1.innerHTML=cstore.value; } function add_to_badcount1(){ var badcount1=document.getElementById('badcount1'); var cstore = document.getElementById('bad_counter_score1'); cstore.value=parseInt(cstore.value)+1; badcount1.innerHTML=cstore.value; } </script> <a href="javascript:add_to_goodcount1();">Good </a><input type="hidden" name="good_counter_score1" id="good_counter_score1" value='0'><span id="goodcount1">0</span> <p> </p> <a href="javascript:add_to_badcount1();">Bad </a><input type="hidden" name="bad_counter_score1" id="bad_counter_score1" value='0'><span id="badcount1">0</span> I'm looking to store the data onto a text file on the server, so that the number will be restored. I'm pretty new to AJAX and PHP, so any help would be greatly appreciated. Thanks!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.