help with cookie regulated poll!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

help with cookie regulated poll!!!

Post by andylyon87 »

Hey guys

I am tryin to code for a poll that allows people to read a review and the vote yes or no as to whether it was helpful. There are yes and no .txt file allocated for each review. The form should read the file and add one onto it. then using cookies it refuses to allow the person to vote again.

Have coded it but it doesn't work and connot work out why, wanted to know how you may have coded it, I am having problems doing the formaction due to the way in which my navigation works, wondered whether you guys knew of any where where I could get this style of script and edit to suit my needs.

Lastly this is the first time I have used cookies but they all appear to be right.

So basically where can I find a poll that is cookie regulated( ie if there is a cookie of the same name as the review it should not display otion to vote)


PHP sofar looks like this:

Code: Select all

<?php
function no(){
$counter_file = 'php/form_data/votes/'.$review.'_no.txt';
if (file_exists($counter_file)){
print("$counter said no.");}else{
print("no previous results");
}}

function yes(){
$counter_file = 'php/form_data/votes/'.$review.'_yes.txt';
if (file_exists($counter_file)){
print("$counter said yes.");}else{
print("no previous results");
}}

createform(){
print("<center><table cellspacing=0 cellpadding=0 width=540 border=0>
<TR><TD>");
yes();
print(" - ");
no();
print("</TD></TR>
<FORM action="?id=vote_handler" method=post name=review_n>
<TR><TD>Was this Review Helpful:</TD></TR>
<TR><TD>YES<input type=radio name=yes>&nbsp;NO<INPUT TYPE=RADIO Name=no></TD></TR>
<TR><TD><input type=hidden name=beensubmitted value=true></TD></TR>
<TR><TD><CENTER><INPUT TYPE=SUBMIT value=Submit! name=Submit></TABLE>");}

if($_COOKIE[$review]){
print("<center><table cellspacing=0 cellpadding=0 width=540 border=0>
<TR><TD>Sorry You Have Already Voted For This Review</TD></TR></TABLE>");
}else{
createform();
}
?>
the handler (haven't got up to testin this page yet so could be riddled with crapups:

Code: Select all

<?php
if($beensubmitted){
if($yes){
$counter_file = 'php/form_data/votes/'.$review.'_yes.txt';
if (file_exists($counter_file)){
$counter = file_get_contents($counter_file);
if (is_numeric($counter)){$counter++;}else{$counter = 0;}
$fp = fopen($counter_file, 'w');
fwrite($fp, $counter);
fclose($fp);}
setcookie($review, "", 3600);
print("Thank you for your time your entry has been submitted");}

if($no){
$counter_file = 'php/form_data/votes/'$review.'_no.txt';
if (file_exists($counter_file)){
$counter = file_get_contents($counter_file);
if (is_numeric($counter)){$counter++;}else{$counter = 0;}
$fp = fopen($counter_file, 'w');
fwrite($fp, $counter);
fclose($fp);}
setcookie($review, "", 3600);
print("Thank you for your time your entry has been submitted");}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your use of $review inside the functions won't work. You should pass the value into the function as an argument, or access it through the globals. (I'd suggest passing it in)
Post Reply