Jump to content

PHP ajax poll script


bindiya

Recommended Posts

I have a  php poll page where the users need to cast their votes and the resut will be showed in a pie chart

 

The questions  and are taken from the database table poll_questions

When the user cast his vote ,the vote for which answer along with the question should go to the database table poll_votes.

 

I am using ajax for this. I am able to insert the vote to the database.

but not able to insert the question to the database.

 

Pasting my code here

 

poll_contest.php
<?php
include ('config.php');
?>
<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&         +"&question="+q
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("poll").innerHTML=xmlhttp.responseText;
    }
  }
  var url="poll_vote.php";
url=url+"?vote="+int;
//url=url+"&sid="+Math.random();
  //var url="poll_vote.php?vote="+int+"&question="+q ;                                   +"&question="+q
  //alert(q);
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="poll">

<?php
$ip=$_SERVER['REMOTE_ADDR'];


echo "<table>";
$q="select * from polls_questions where id=(select max(id) from  polls_questions)";
$r=mysql_query($q);
$row=mysql_fetch_row($r);
$poll_id = $row['id'];
$question=$row['question'];
echo "<input type='hidden' name='question' id='question'  value=$question>";
echo "<tr><td>$row[1]</td></tr>".
"<tr><td><input type='radio' value='1' name='answer' id='answer' onclick='getVote(this.value)'>$row[2]</td></tr>".
"<tr><td><input type='radio' value='2' name='answer' id='answer' onclick='getVote(this.value)'>$row[3]</td></tr>".
"<tr><td><input type='radio' value='3' name='answer' id='answer' onclick='getVote(this.value)'>$row[4]</td></tr>".
"<tr><td><input type='radio' value='4' name='answer' id='answer' onclick='getVote(this.value)'>$row[5]</td></tr>".
"</table>";




?>

</body>
</html>



poll_vote.php

<?php
include ('config.php');
$ip=$_SERVER['REMOTE_ADDR'];
echo $ip;

$vote = $_GET['vote'];
$question=$_GET['question'];
echo $vote;
echo "<br>".$question;

?>



 

 

Link to comment
https://forums.phpfreaks.com/topic/234227-php-ajax-poll-script/
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.