Hi guys,
I was trying to create a php polling system which reads its variables from a txt file. However, I keep getting the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/vote.php:2) in /home/mysite/public_html/vote.php on line 31
Below’s my source code:
/////////////////test.html//////////////////////////
<div align="left" id="votediv">
<p align="left">Which of the following game is your favourite?</p>
<form method="post" action="vote.php">
<table width="241" height="164" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="38" height="66"><p>
<label></label>
</p></td>
<td width="203"><p>
<label>
<input name="vote" type="radio" value="0" checked="checked"/>
DoTA 3v3</label>
</p>
<p>
<label>
<input name="vote" type="radio" value="1" />
DoTA 4v4</label>
</p>
<p>
<label>
<input name="vote" type="radio" value="2" />
Counter-Strike 1.5</label>
</p>
<p>
<label>
<input name="vote" type="radio" value="3" />
Counter-Strike 1.6</label>
</p>
<p>
<label>
<input name="vote" type="radio" value="4" />
Counter-Strike: Source</label>
</p></td>
</tr>
<tr>
<td height="64" colspan="2"><div align="right">
<input name="submit" type="image" value="Vote" class="submitbtn" src="images/submit_03.gif"/>
</div></td>
</tr>
</table>
</form>
<p align="left"> </p>
</div>
//////////////////vote.php//////////////////////
<?php
$vote = $HTTP_POST_VARS['vote'];
//the filename
$filename = "txt/vote.txt";
//get content of textfile
$content = file($filename);
$array = explode("||", $content[0]);
$dota3 = $array[0];
$dota4 = $array[1];
$cs15 = $array[2];
$cs16 = $array[3];
$css = $array[4];
if($vote == 0) $dota3 = $dota3 + 1;
if($vote == 1) $dota4 = $dota4 + 1;
if($vote == 2) $cs15 = $cs15 + 1;
if($vote == 3) $cs16 = $cs16 + 1;
if($vote == 4) $css = $css + 1;
$insertvote = $dota3."||".$dota4."||".$cs15."||".$cs16."||".$css;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
header("Location: stats.php");
?>
//////////////////stats.php////////////////////
<?php
//the filename
$filename = "txt/vote.txt";
//get content of textfile
$content = file($filename);
$array = explode("||", $content[0]);
$dota3 = $array[0];
$dota4 = $array[1];
$cs15 = $array[2];
$cs16 = $array[3];
$css = $array[4];
?>
I’ve also created a txt folder to store the text file. The ‘vote.txt’ file contains the following values:
0||0||0||0||0
Any help is greatly appreciated. Thanks in advance.