Log in

View Full Version : Quiz with php and without sql



Acunga
12-28-2009, 09:34 AM
http://www.w3schools.com/php/php_ajax_poll.asp
hello guys i say this threat and tried to implement it but it does not seem to show any proper results and does not edit the text file.
What I am doing wrong?
Also I would like it not to load the question every time i refresh and the user should be allowed to vote once

djr33
12-28-2009, 05:53 PM
You need some way of storing the data on the server. You don't necessarily need sql, or even any database, but if not, then you need it to store in text files. Either way, you must store the IP or username of the users who have voted, and there is no way around that. You could place a cookie on their computer so they could only vote once per session, but a user who knows a bit about computers could delete it and vote repeatedly easily. It looks like the tutorial uses files, so that's fine, but you'll need to add a few options to make that fully work.

If you want help with this, you need to post your code in addition to the link the tutorial you are using. There is something wrong with how you used that code, not with the tutorial. It is possible that your server just doesn't work with something, such as with php modifying files, though. But we can't help without information about your site.

Acunga
12-30-2009, 10:49 AM
thank you very much for the reply, I figured out I need a server program and now I use wampserver.I'm a newb so there is no way I could knew about that.
So anyway i modified the code for a couple of days but with my limited knowledge in php and java script I can't figure out how to prevent users from voting more then once with the ip algorithm.I need an algorithm to check if a given ip have voted and if it did, i need to show up results directly, not showing any messages.
I am trying with cookies right now, cuz it seems to be easier and i have lost too much time already so i would be happy on any sort of solution that prevents double voting at least on 1-rst level:

<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);

$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if( isset( $_COOKIE['vote']))
{
?>
<div id="quiz"><a href="#">Did you know...</a></div>
<div id="quiz2">
<h2 >Did you know Ring of Slaying has a right-click option to check kills?</h2>
<table cellpadding="0px" cellspacing="0px" style="background:#CEB98E;font-weight:normal;" >
<tr>
<td>Yes:</td>
<td>
<img style="margin:0px;padding:0px;"src="poll.gif"
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.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
</div>
<?php

}
else{

?>
<div id="poll">
<div id="quiz"><a href="#">Did you know...</a></div>
<div id="quiz2">
<h2>Ring of Slaying has a right-click option to check kills?</h2>

<a name="vote" href="javascript:getVote(0)">Yes</a>

<a name="vote" href="javascript:getVote(1)">No</a>

</div>
</div>
<?php
}


?>

javascr. file:

function commonfunca(){
setcookie(vote);
getVote(0)
}

function commonfuncb(){
setcookie(vote);
getVote(1)
}

function setcookie ("vote", "vote", )
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;
}
}

function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
my idea is to set commonfunca() which start the two functions getvote and setcookie and after that to check with php if the cookie was set and show the results if it is.
But the problem is that I can't figure out how to set a cookie in javascript :eek::eek:
I would be very happy if somebody can replace the example with real values which I can replece with my own, cuz I don't know if javascript want quotations, or no quotations:

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
thanks and sorry for the stupid question.