Jump to content

PHP Poll


davidpeters

Recommended Posts

Hello! I am fairly new to PHP, so pardon any of my 'newbie' mistakes. I am trying to create a simple poll which allows three things. I would like users to be able to vote and their submissions be recorded into a text file. I am also interested in disabling multiple votes, which basically means I want the user's IP address recorded so they can only vote once. I found a great tutorial here (http://net.tutsplus.com/tutorials/javascript-ajax/creating-a-dynamic-poll-with-jquery-and-php/), and downloaded the package including the data and the source code. However, the votes don't seem to be recording on my site. The site I am currently working on is located at http://davidpeters.freeiz.com/music.htm The actual poll's code looks something like this:

<div id="container">	  <div id="left">    <div id="poll-container">	 <h3>Poll</h3>	 <form id='poll' action="poll.php" method="post" accept-charset="utf-8">	  <p>Pick your favorite Javascript framework:</p><p>	  <input type="radio" name="poll" value="opt1" id="opt1" /><label for='opt1'> Option 1</label><br />	  <input type="radio" name="poll" value="opt2" id="opt2" /><label for='opt2'> Ext JS</label><br />	  <input type="radio" name="poll" value="opt3" id="opt3" /><label for='opt3'> Dojo</label><br />	  <input type="radio" name="poll" value="opt4" id="opt4" /><label for='opt4'> Prototype</label><br />	  <input type="radio" name="poll" value="opt5" id="opt5" /><label for='opt5'> YUI</label><br />	  <input type="radio" name="poll" value="opt6" id="opt6" /><label for='opt6'> mootools</label><br /><br />	  <input type="submit" value="Vote →" /></p>	 </form>    </div>   </div>

And the PHP behind it looks like this:

<?php// Poll option definitions$options[1] = 'jQuery';$options[2] = 'Ext JS';$options[3] = 'Dojo';$options[4] = 'Prototype';$options[5] = 'YUI';$options[6] = 'mootools';// Column definitionsdefine('OPT_ID', 0);define('OPT_TITLE', 1);define('OPT_VOTES', 2);define('HTM_FILE', 'music.htm');// Initialize the DBrequire_once('flatfile.php');$db = new Flatfile();$db->datadir = 'data/';define('VOTE_DB', 'votes.txt');define('IP_DB', 'ips.txt');if ($_GET['poll'] || $_POST['poll']) {  poll_submit();}else if ($_GET['vote'] || $_POST['vote']) {  poll_ajax();}else {  poll_default();}function poll_ajax() {  global $db;  global $options;  $id = $_GET['vote'] || $_POST['vote'];   $ip_result = $db->selectUnique(IP_DB, 0, $_SERVER['REMOTE_ADDR']);   if (empty($ip_result)) {    $ip[0] = $_SERVER['REMOTE_ADDR'];    $db->insert(IP_DB, $ip);       if ($id != 'none') {	  $row = $db->selectUnique(VOTE_DB, OPT_ID, $id);	  if (!empty($row)) {	    $new_votes = $row[OPT_VOTES]+1;   	    $db->updateSetWhere(VOTE_DB, array(OPT_VOTES => $new_votes), new SimpleWhereClause(OPT_ID, '=', $id));	  }	  else if ($options[$id]) {	    $new_row[OPT_ID] = $id;	    $new_row[OPT_TITLE] = $options[$id];	    $new_row[OPT_VOTES] = 1;	    $db->insert(VOTE_DB, $new_row);	  }    }  }   $rows = $db->selectWhere(VOTE_DB, new SimpleWhereClause(OPT_ID, "!=", 0), -1, new OrderBy(OPT_VOTES, DESCENDING, INTEGER_COMPARISON));  print json_encode($rows);}function poll_submit() {  global $db;  global $options;   $id = $_GET['poll'] || $_POST['poll'];  $id = str_replace("opt", '', $id);   $ip_result = $db->selectUnique(IP_DB, 0, $_SERVER['REMOTE_ADDR']);   if (!isset($_COOKIE['vote_id']) && empty($ip_result)) {    $row = $db->selectUnique(VOTE_DB, OPT_ID, $id);    if (!empty($row)) {	  $ip[0] = $_SERVER['REMOTE_ADDR'];	  $db->insert(IP_DB, $ip);	 	  setcookie("vote_id", $id, time()+31556926); // cookie expires in one year	 	  $new_votes = $row[OPT_VOTES]+1;	  $db->updateSetWhere(VOTE_DB, array(OPT_VOTES => $new_votes), new SimpleWhereClause(OPT_ID, '=', $id));	 	  poll_return_results($id);    }    else if ($options[$id]) {	  $ip[0] = $_SERVER['REMOTE_ADDR'];	  $db->insert(IP_DB, $ip);	 	  setcookie("vote_id", $id, time()+31556926); // cookie expires in one year	 	  $new_row[OPT_ID] = $id;	  $new_row[OPT_TITLE] = $options[$id];	  $new_row[OPT_VOTES] = 1;	  $db->insert(VOTE_DB, $new_row);	 	  poll_return_results($id);    }  }  else {    poll_return_results($id);  }}function poll_default() {  global $db;   $ip_result = $db->selectUnique(IP_DB, 0, $_SERVER['REMOTE_ADDR']);   if (!isset($_COOKIE['vote_id']) && empty($ip_result)) {    print file_get_contents(HTML_FILE);  }   else {    poll_return_results($_COOKIE['vote_id']);  }}function poll_return_results($id = NULL) {    global $db;     $html = file_get_contents(HTML_FILE);    $results_html = "<div id='poll-container'><div id='poll-results'><h3>Poll Results</h3>\n<dl class='graph'>\n";       $rows = $db->selectWhere(VOTE_DB,	  new SimpleWhereClause(OPT_ID, "!=", 0), -1,	  new OrderBy(OPT_VOTES, DESCENDING, INTEGER_COMPARISON));    foreach ($rows as $row) {	  $total_votes = $row[OPT_VOTES]+$total_votes;    }       foreach ($rows as $row) {	  $percent = round(($row[OPT_VOTES]/$total_votes)*100);	  if (!$row[OPT_ID] == $id) {	    $results_html .= "<dt class='bar-title'>". $row[OPT_TITLE] ."</dt><dd class='bar-container'><div id='bar". $row[OPT_ID] ."'style='width:$percent%;'> </div><strong>$percent%</strong></dd>\n";	  }	  else {	    $results_html .= "<dt class='bar-title'>". $row[OPT_TITLE] ."</dt><dd class='bar-container'><div id='bar". $row[OPT_ID] ."' style='width:$percent%;background-color:#0066cc;'> </div><strong>$percent%</strong></dd>\n";	  }    }       $results_html .= "</dl><p>Total Votes: ". $total_votes ."</p></div></div>\n";       $results_regex = '/<div id="poll-container">(.*?)<\/div>/s';    $return_html = preg_replace($results_regex, $results_html, $html);    print $return_html;}if (!function_exists('json_encode')) { // json_encode was added in 5.2.0, this is a replacement function if the PHP version is old  function json_encode($a=false) {    if (is_null($a)) return 'null';    if ($a === false) return 'false';    if ($a === true) return 'true';    if (is_scalar($a)) {	  if (is_float($a)) {	    // Always use "." for floats.	    return floatval(str_replace(",", ".", strval($a)));	  }	  if (is_string($a)) {	    static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));	    return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';	  }	  else	    return $a;    }    $isList = true;    for ($i = 0, reset($a); $i < count($a); $i++, next($a)) {	  if (key($a) !== $i) {	    $isList = false;	    break;	  }    }    $result = array();    if ($isList) {	  foreach ($a as $v) $result[] = json_encode($v);	  return '[' . join(',', $result) . ']';    }    else {	  foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);	  return '{' . join(',', $result) . '}';    }  }}

Any suggestions as to why it may not be recording? Any response is greatly appreciated.

Link to comment
Share on other sites

Hello, As I see this is your first post I want to let you know that posting code that you got from another site and asking why it doesn't work generally doesn't fly.Most members won't help if you haven't tried to fix the problem yourself.So tell us, what have you tried to fix the issue? ~Krewe

Link to comment
Share on other sites

is there any visible error?append it in front of the file to report all kind of error

error_reporting(E_ALL); ini_set('display_error',1);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...