Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1706 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

PHP Poll Problem

Thread title: PHP Poll Problem
Closed Thread    
    Thread tools Search this thread Display Modes  
12-11-2005, 10:03 PM
#1
Shawon is offline Shawon
Shawon's Avatar
Status: Request a custom title
Join date: Nov 2004
Location: New York City
Expertise:
Software:
 
Posts: 1,164
iTrader: 0 / 0%
 

Shawon is on a distinguished road

Send a message via AIM to Shawon Send a message via MSN to Shawon

  Old  PHP Poll Problem

Hey, having a little problem with my poll, wondering if anyone can help.

So I have this poll, I didn't write it, it was a free script. It's at http://www.nflxl.com/ (on the right side).

The problem is when you vote it doesn't do anything. It used to work on my old server, but recently I moved to a new server and it doesn't work. Same thing happened with my shoutbox and comment system, but I got those fixed, but not sure how to fix this poll.

The votes go into a txt file, and yes I do have it CHMODed to 777 so I dunno what's wrong. There are 3 files: index.php, poll.block.php, and poll.class.php. Here are the php codes:

index.php
PHP Code:
<?php
    
require("poll.class.php");
    
$my_poll = new Poll();
    
    
$question "Which team has had the most unexpected season so far?";
    
$answers = array("Patriots","Bengals","Giants","Cowboys","Packers");
    
$datafile "poll/polls/unexpected.txt";

    
$my_poll->set($question$answers$datafile);
    
$my_poll->create();
?>
poll.block.php
PHP Code:
<?php
    
if($cmd == "vote"){
        if(!isset(
$pollblock) && !headers_sent()){
        
setcookie("pollblock""block"0"/");
    }
    }
?>
poll.class.php
PHP Code:
<?php
class Poll {
    var 
$questions;
    var 
$answers;
    var 
$datafiles;
    function 
Poll(){
        
$this->questions = array();
        
$this->answers = array();
        
$this->datafiles = array();
    }
    function 
set($question$answer$datafile){
        
array_push($this->questions$question);
        
array_push($this->answers$answer);
        
array_push($this->datafiles$datafile);
    }
    function 
create(){
        
$PHP_SELF $_SERVER['PHP_SELF'];
        global 
$PHP_SELF$HTTP_POST_VARS$pollblock$cmd;
        if(
$cmd == "vote"){
        
// ----------------------------------------------------------------------
        // * DISPLAY RESULTS *
                
                
if(isset($pollblock)){
                echo 
"<p>Sorry, it appears that you have already voted.</p>";
                return;
            }
            for(
$i 0$i count($this->questions); $i ++){
                
$question $this->questions[$i];
                
$answer $this->answers[$i];
                
$datafile $this->datafiles[$i];
                
$vote $HTTP_POST_VARS["poll".$i];
                
$data file($datafile);
                
                
// Populate default values - only happens on first vote
                
if(!$data[0]){
                    
$fp fopen($datafile"w");
                    
$defaultvalues "";
                    for(
$k 0$k count($answer); $k ++){
                        
$defaultvalues .= "0\n";
                    }
                    
flock($fp1);
                    
fputs($fp$defaultvalues);
                    
flock($fp3);
                    
fclose($fp);
                }
                
                
// Write votes to datafile
                
if(isset($vote)){
                    
$oldvotes $data[$vote];
                    
$oldvotes preg_replace("/\n\r*/"""$oldvotes);
                    
$data[$vote] = ($oldvotes 1)."\n";
                    
$file implode(""$data);
                    
$fp fopen($datafile"w");
                    
flock($fp1);
                    
fputs($fp$file);
                    
flock($fp3);
                    
fclose($fp);
                }
                
                
// Display Results
                
$total 0;
                while(list(
$key$val) = each($data)){
                    
$total += $val;
                }
                echo 
"<h2>$question</h2>\n";
                echo 
"<p>\n";
                while(list(
$key$val) = each($answer)){
                    
$votes preg_replace("/\n\r*/"""$data[$key]);
                    
$percent = ($votes 0) ? round(($votes/$total)*100) : 0;
                    if(!
$votes$votes 0;
                    echo 
""$answer[$key] ." <span class=\"winner\">("$votes .")</span><br />\n";
                }
                echo 
"</p>\n";
            }
        
// ----------------------------------------------------------------------
        
}else{
        
// ----------------------------------------------------------------------
        // * DISPLAY POLL *
            
            
echo "<form action=\"$php_self\" method=\"post\">\n";
            echo 
"<input type=\"hidden\" name=\"cmd\" value=\"vote\" />\n";
            for(
$i 0$i count($this->questions); $i ++){
                
$question $this->questions[$i];
                
$answer $this->answers[$i];
                echo 
"<h2>$question</h2>\n";
                echo 
"<p>";
                while(list(
$key$val) = each($answer)){
                    echo 
"<input type=\"radio\" class=\"radio\" name=\"poll$i\" value=\"$key\" />$val<br />\n";
                }
                echo 
"</p>\n";
            }
            echo 
"<div align=\"center\"><input type=\"submit\" class=\"submit\" name=\"vote\" value=\"Vote\" /></div>\n";
            echo 
"</form>\n";
        
        
// ----------------------------------------------------------------------
        
}
    }
}
?>

12-11-2005, 11:05 PM
#2
htmlmaster is offline htmlmaster
Status: Member
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 325
iTrader: 0 / 0%
 

htmlmaster is on a distinguished road

Send a message via AIM to htmlmaster

  Old

Forgive me if I'm wrong, I only looked at the code for a second, but I don't see any MySQL in there. Are you sure you have all the files? MySQL is pretty much essential for keeping track of data.

12-11-2005, 11:16 PM
#3
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

htmlmaster, not every PHP script requires the use of a MySQL database. *sigh* The script uses a plain text file to store data.

The votes go into a txt file

12-11-2005, 11:51 PM
#4
Shawon is offline Shawon
Shawon's Avatar
Status: Request a custom title
Join date: Nov 2004
Location: New York City
Expertise:
Software:
 
Posts: 1,164
iTrader: 0 / 0%
 

Shawon is on a distinguished road

Send a message via AIM to Shawon Send a message via MSN to Shawon

  Old

Lol yeah, as stated in my post, it goes into a flatfile.

12-12-2005, 01:37 AM
#5
htmlmaster is offline htmlmaster
Status: Member
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 325
iTrader: 0 / 0%
 

htmlmaster is on a distinguished road

Send a message via AIM to htmlmaster

  Old

Sorry, didn't read the full post. Whenever I make PHP polls, I use a database, foolish assumption.

12-12-2005, 02:05 AM
#6
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

I've only had a brief look at it, and currently have no localhost to test the script myself but the working-on-one-server-but-not-another might be a clue. Sometimes this is because 'register globals' is on for server A and not for server B. This script relies on the setting being turned on, but for security purposes many hosts nowadays switch it off.

If that is the case, then checking that the form has been submitted might not actually be happing as expected. Why? The script relies on the $cmd variable being set to vote. If register globals is turned off, then $cmd won't actually be set to anything (indeed it won't exist)!

As a stop-gap measure to see if this was actually the problem, try adding the following to the top of index.php (above the require line):
PHP Code:
$cmd $_POST['cmd']; 
It might also be helpful to (temporarily) turn on all error messages generated by PHP by adding the following line, again above the require.
PHP Code:
error_reporting(E_ALL); 
P.S. Robson, you really need to sort out these code boxes!

12-12-2005, 03:02 AM
#7
Shawon is offline Shawon
Shawon's Avatar
Status: Request a custom title
Join date: Nov 2004
Location: New York City
Expertise:
Software:
 
Posts: 1,164
iTrader: 0 / 0%
 

Shawon is on a distinguished road

Send a message via AIM to Shawon Send a message via MSN to Shawon

  Old

omg i love you

i asked some ppl they said something bout registered globals too and also for the shoutbox i did something like that i had to define the variables again with $_POST and it worked, but wasnt sure how to do it on the poll one, not that experienced with php

but it works now, thanks alot man appreciate it

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed