Jump to content

Problem With Ajax Poll


MadFly
 Share

Recommended Posts

I have copied the codes into the file names as instructed, but when i try to execute it, i get this error message...error_with_poll.JPGThis is where i got the code from http://www.w3schools.com/php/php_ajax_poll.asphtml page is in index.htmljavascript code is in poll.jsand php code is in poll_vote.phplink to page:http://www.wernercoetzee.co.za/test_poll/

Edited by MadFly
Link to comment
Share on other sites

does it work with just the first questions, only?id="poll" is only supposed to be once, class can be used multiple times.the example is for 1 poll question only, to include more will require amending the code to accomodate passing more values, depending on waht to want it to do?

Edited by dsonesuk
Link to comment
Share on other sites

thanksit gives the exact same error with one poll question...I have changed it to one question again.I would like to create a poll under some information on a page. I have a page wherei try to give users very basic pc help, on how to fix certain basic computerproblems, and then at the bottom of that text i would like to put a poll about wasthis information helpfull or not?or something in that line...what does that 'xmlHttp' is undefined mean? and is there a way to fix it?

Link to comment
Share on other sites

i just downloaded the exact code from example and it does not work, i then got hold of the javascript file used in example, and its different? i used this code and it works now! BAD, BAD w3schools. any way try thisvar xmlHttpfunction getVote(int){xmlHttp=GetXmlHttpObject()if (xmlHttp==null){alert ("Browser does not support HTTP Request")return} var url="poll_vote.php"url=url+"?vote="+inturl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true)xmlHttp.send(null)} function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ document.getElementById("poll").innerHTML=xmlHttp.responseText;} } function GetXmlHttpObject(){ var objXMLHttp=nullif (window.XMLHttpRequest){objXMLHttp=new XMLHttpRequest()}else if (window.ActiveXObject){objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")}return objXMLHttp}

Edited by dsonesuk
Link to comment
Share on other sites

the error is because the "xmlHttp" in document.getElementById("poll").innerHTML=xmlHttp.responseText; has an uppercase 'H' it should be all lowercase as the variable statement at the beginning (must have former MS employee, who does not test code).var xmlhttp;function getVote(int){xmlhttp=GetXmlHttpObject();if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; }var url="poll_vote.php";url=url+"?vote="+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("poll").innerHTML=xmlHttp.responseText; }}

Edited by dsonesuk
Link to comment
Share on other sites

you will have to create a button that will run the javascript file when clicked, instead of the javascript being run when a radio button is clicked, as you require all questions answered before this.you will also need to pass the answer values to javascript, then php, and then store the vote totals individually in a array within a text file.so its a lot of recoding to achieve this, but it can be done.

Link to comment
Share on other sites

ooooo okay!Sounds like alot of work... but i will see if i can manage to get that right.If not, i hope you will be willing to help me with this?If it is not too much asked, could you recode the code for 2 questions? then i can just add more.But i'll first see if i can get something going...

Link to comment
Share on other sites

This is what i have so far<input type="button" onclick="poll.js" value="View Results" />But when i click on it, nothing happens.

you will have to create a button that will run the javascript file when clicked, instead of the javascript being run when a radio button is clicked, as you require all questions answered before this.you will also need to pass the answer values to javascript, then php, and then store the vote totals individually in a array within a text file.
How do i get that done?You can view it at http://www.wernercoetzee.co.za/test_poll/polls/index.html
Link to comment
Share on other sites

I think i got it...I made the View Results to open seperately in results.php, and then i added a backbutton to go back to the poll page.is there a way to make the result page open on the same page as it would when you click on yes or no?

Link to comment
Share on other sites

i've done html, and javascript, i don't know when i can get to do php

<html><head><script type="text/javascript" src="poll.js"></script><script type="text/javascript">function process(){var storeques= new Array();//must match input radio names depending on questions askedstoreques[0]="vote";storeques[1]="vote2";storeques[2]="vote3";storeques[3]="vote4";var findradio;var radiovalue="";var storeparam="";//search through radio button group using values in arrayfor(j=0;j<storeques.length;j++)   {   findradio=document.getElementsByName(storeques[j]);   l=findradio.length;   for(i=0;i<l;i++) //search through individual radio buttons for selected      {      if(findradio[i].checked)         {         radiovalue=findradio[i].value;          }      }    if (j>0 || j+1==storeques.length) //create values to send to poll.js         {        storeparam=storeparam+"&"+storeques[j]+"="+radiovalue;         }     else         {        storeparam=storeparam+storeques[j]+"="+radiovalue;         }   }//alert(storeparam+"&totalques="+storeques.length)getVote(storeparam+"&totalques="+storeques.length) //send values to javascript function}</script></head><body><div id="poll"><h3>Do you like PHP and AJAX so far?</h3><form><input type="radio" name="vote" value="0">No<br /><input type="radio" name="vote" value="1">Yes<br /><br /><input type="radio" name="vote2" value="0">No<br /><input type="radio" name="vote2" value="1">Yes<br /><br /><input type="radio" name="vote3" value="0">No<br /><input type="radio" name="vote3" value="1">Yes<br /><br /><input type="radio" name="vote4" value="0">No<br /><input type="radio" name="vote4" value="1">Yes<br /><br /><input type="button" value="Process Votes" onclick="process()"></form></div></body></html>

java script:

// JavaScript Documentvar xmlHttpfunction getVote(int){xmlHttp=GetXmlHttpObject()if (xmlHttp==null){alert ("Browser does not support HTTP Request")return} var url="poll_vote.php"url=url+"?"+inturl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true)xmlHttp.send(null)} function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ document.getElementById("poll").innerHTML=xmlHttp.responseText;} } function GetXmlHttpObject(){ var objXMLHttp=nullif (window.XMLHttpRequest){objXMLHttp=new XMLHttpRequest()}else if (window.ActiveXObject){objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")}return objXMLHttp}

Link to comment
Share on other sites

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript" src="poll.js"></script><script type="text/javascript"><!--//function process(){var quesnum=4; //must match input radio names depending on questions askedvar findradio;var radiovalue="";var storeparam="";//search through radio button group using values in arrayfor(j=0;j<quesnum;j++)   {   findradio=document.getElementsByName("vote["+j+"]");   l=findradio.length;   for(i=0;i<l;i++) //search through individual radio buttons for selected      {      if(findradio[i].checked)         {         radiovalue=findradio[i].value;          }      }    if (j>0 || j+1==quesnum) //create values to send to poll.js         {        storeparam=storeparam+"&vote["+j+"]="+radiovalue;         }     else         {        storeparam=storeparam+"vote["+j+"]="+radiovalue;         }   }//alert(storeparam+"&totalques="+quesnum)getVote(storeparam+"&totalques="+quesnum) //send values to javascript function including number of questions}//--></script></head><body><div id="poll"><form action=""><h4>Do you like Question#1?</h4><input type="radio" name="vote[0]" value="0" />No<br /><input type="radio" name="vote[0]" value="1" />Yes<br /><br /><h4>Do you like Question#2?</h4><input type="radio" name="vote[1]" value="0" />No<br /><input type="radio" name="vote[1]" value="1" />Yes<br /><br /><h4>Do you like Question#3?</h4><input type="radio" name="vote[2]" value="0" />No<br /><input type="radio" name="vote[2]" value="1" />Yes<br /><br /><h4>Do you like Question#4?</h4><input type="radio" name="vote[3]" value="0" />No<br /><input type="radio" name="vote[3]" value="1" />Yes<br /><br /><input type="button" value="Process Votes" onclick="process()" /></form></div><div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui.</div></body></html>

javascript

  // JavaScript Documentvar xmlHttpfunction getVote(int){xmlHttp=GetXmlHttpObject()if (xmlHttp==null){alert ("Browser does not support HTTP Request")return} var url="poll_vote.php"url=url+"?"+inturl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true)xmlHttp.send(null)} function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ document.getElementById("poll").innerHTML=xmlHttp.responseText;} } function GetXmlHttpObject(){ var objXMLHttp=nullif (window.XMLHttpRequest){objXMLHttp=new XMLHttpRequest()}else if (window.ActiveXObject){objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")}return objXMLHttp}   

php

  <?php$totalques = $_REQUEST['totalques'];$countvotes=0;foreach ($_REQUEST['vote'] as $val)    {    $votelist[$countvotes]= $val;    $countvotes++;    }//get content of textfile$filename = "poll_result.txt";$content = file($filename);//put content in array$array = explode("||", $content[0]);for($i=0;$i<$totalques;$i++)    {                $vote=$votelist[$i];        if($vote==1)            {            if($i>0)                {                $yes=$array[($i*2)];                                 $array[($i*2)] = $yes+1;                }            else                {                $yes = $array[$i];                $array[$i] = $yes+1;                }            }                    if($vote==0) //NO Vote Correct DO NOT AMEND            {            if($i>0)                {                $no = $array[$i*2+1];                $array[$i*2+1] = $no+1;                }            else                {                $no = $array[$i+1];                                $array[$i+1] = $no+1;                }            }                }//insert votes to txt filefor ($i=0;$i<($totalques*2);$i++)    {    $vote = $array[$i];    if($vote==""){$vote="0";}    $insertvote=$insertvote."".$vote;    if(($i)<($totalques*2)-1)        {        $insertvote=$insertvote."||";        }    }$fp = fopen($filename,"w");fputs($fp,$insertvote);fclose($fp);//create heading for results$header[0]="<h3>Poll#1 Result:</h3>";$header[1]="<h3>Poll#2 Result:</h3>";$header[2]="<h3>Poll#3 Result:</h3>";$header[3]="<h3>Poll#4 Result:</h3>";?><table border="1" width="100%" style="border-collapse:collapse;"><tr><?php $tempques=$totalques;for($count=0;$count<$tempques;$count++){echo "<td colspan=\"4\">".$header[$count]."</td>";}echo "</tr><tr>";for($i=0;$i<$totalques*2;$i++){ $yes = $array[$i];$no = $array[$i+1];?><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><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)); ?>%<br /></td><?php $i++; } ?></tr></table>   

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
 Share

×
×
  • Create New...