Location: PHPKode > scripts > AJAX Voting Poll System > VotingPoll.AJAX/Distribution/VotingPoll.AJAX/Scripts/VotingPoll.AJAX.php
<?php
	
	/*
		VotingPoll.AJAX Server Routine [version 1.0]

		http://acidjs.wemakesites.net/voting-poll.html
	*/

	$pollquestion = "";
	$answers = "";
	
	function read_results(){
		global $pollquestion, $answers;
		$pollfile = file("../Polls/".$_POST["datafile"]."");
		$pollquestion = $pollfile[0];

		$numberOfAnswers = sizeof($pollfile) - 1;
		$count = 0;
		for ($i = 1; $i <= $numberOfAnswers; $i ++){
			$answerData = explode("|", $pollfile[$i]);
			
			if (strlen(trim($answerData[0])) > 0){
				$answers[$count]["text"] = $answerData[0];
				$answers[$count]["count"] = $answerData[1];
				++ $count;
			}
		}
	}

	read_results();
?>	
<code><!-- / --></code>
<form>
	<fieldset>
		<?
			if (!isset($_POST["submit-vote"])){
			echo "<legend>".$pollquestion."</legend>"; 
		?>
		<ul class="poll-questions">
		<?php
			foreach ($answers as $value)
			{
				echo "<li> <input type=\"radio\" name=\"".$_POST["polling"]."\" id=\"".$value["text"]."\" title=\"".$value["text"]."\" value=\"".$value["text"]."\"/> <label title=\"".$value["text"]."\" for=\"".$value["text"]."\">".$value["text"]."</label> </li>";
			}
		?>
		</ul>
		<div class="submit-button">
			<input type="submit" name="submit-vote" title="<?php echo $_POST["submitbuttontext"] ?>" value="<?php echo $_POST["submitbuttontext"] ?>" />
		</div>
	</fieldset>
</form>
	<?php
	}
	else
	{
		$count = 0;
		foreach ($answers as $value)
		{
			if ($value["text"]  == $_POST["polling"])
			{
				$answers[$count]["count"] = ((int)$value["count"]) + 1;
				(int)$totalCount ++;
		}
		++ $count;
	}
		   
	global $pollquestion, $answers;
	
	$file = fopen("../Polls/".$_POST["datafile"], "w");
	
	fwrite($file, $pollquestion."\r\n",strlen($pollquestion));
	foreach ($answers as $value)
	{
		$row = $value["text"]."|".$value["count"]."\r\n";
		fwrite($file, $row, strlen($row));
	}
	fclose($file);
	
	echo "<legend>".$_POST["thankyouforvotingmessage"]."</legend>";
	echo "<ul class=\"bar-graph\">";
	foreach ($answers as $value)
	{
		echo "<li unselectable=\"on\"><span title=\"".$value["text"]."\" class=\"text\" unselectable=\"on\"><span unselectable=\"on\">".$value["text"]."</span></span><span class=\"bar\" style=\"width: ".(int)$value["count"]."px;\" unselectable=\"on\">&nbsp;</span><span class=\"value\" unselectable=\"on\">".$value["count"]."</span></li>";
	}
	echo "</ul>";
} 
?>
<code><!-- / --></code>
Return current item: AJAX Voting Poll System