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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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