Jump to content

Problems displaying multiple PHP Polls


nielse63

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/174429-problems-displaying-multiple-php-polls/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.