I am working on and testing a fairly simple flat file poll. So far, it works well but I am need of some help resolving a couple of things.
First, once a user submits their vote, I’m trying to have a message appear just below the “Submit My Vote” button to say “Thank you for your vote.” I’ve tried several things and none have worked. Could someone look at my code and advise where I’m going wrong?
Second, you will note, if any one of the poll questions are not answered, a message appears (on a blank page) which notifies the user they must select an answer to that question and resubmit. Again, I would like this message to appear below the “Submit My Vote” button rather than on a blank page. I would also like to have their selections on the other questions populated rather than for them to have to answer all questions again. I’m lost on how to do that. Any help in this regard would also be much appreciated.
No doubt there are other things in this poll that should also be cleaned up or could be accomplished in a more efficient way – but I’m still just a newbie at PHP as well as HTML.
<?php
$file='poll_2.txt';
if(!file_exists($file)) { //if file doesn't exist, try to create
$fp = fopen($file, 'w');
$close = fclose($fp);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Poll Questions</title>
</head>
<body>
<?php
//if the vote has not been submitted:
if(!isset($_POST['submit']))
{
?>
<!-- START TABLE 1 -->
<table width="300" border="0" cellspacing="0" cellpadding="1" bgcolor="#666699">
<tr align="center">
<td>
<style type="text/css">
<!--
.input {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt}
-->
</style>
<font face="Verdana, Arial, Helvetica, sans-serif" size="-1" color="#FFFFFF"><b>Classmate Poll</b></font></td>
</tr>
<tr align="center">
<td>
<!-- START TABLE 2 -->
<table width="100%" border="0" cellspacing="0" cellpadding="2" align="center" bgcolor="#FFFFFF">
<tr>
<td height="30" valign="middle"><font face="Verdana, Arial, Helvetica, sans-serif" color="red" size="3">
<center><b>Reunion Questionnaire</b></center></font>
</td></tr>
<tr align="right" valign="top">
<td>
<form method="post" action="<?php echo $SERVER['PHP_SELF'];?>">
<script language="JavaScript" type="text/javascript">
<!--
//-->
</script>
<!-- START TABLE 3 -->
<table width="100%" border="1" cellspacing="0" cellpadding="0" align="center">
<tr valign="top" align="center">
<td>
<!-- START TABLE 4 -->
<table width="100%" border="0" celspacing="0" cellpadding="1" align-"center">
<tr>
<td bgcolor="#EBDDE2"><font face="Verdana, Arial, Helvetica, sans-serif" color="black" size="1">
<fieldset><legend><b>Q1: Should We Have Another Reunion?</b></font></legend>
  <input type="radio" name="option1" value="Y">Yes      
  <input type="radio" name="option1" value="N">No
</fieldset></td></tr>
<tr><td bgcolor="#ECE5B6"><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000" size="1">
<fieldset><legend><b>Q2: What Year?</b></legend>
  <input type="radio" name="option2" value="09">2009    
  <input type="radio" name="option2" value="10">2010<br>
  <input type="radio" name="option2" value="11">2011    
  <input type="radio" name="option2" value="12">2012<font size="1"> (45th Anniversary)<br>
</fieldset></td></tr>
<tr><td bgcolor="#EBDDE2"><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000" size="1">
<fieldset><legend><b>Q3: Another Reunion Should Be:</b></legend>
  <input type="radio" name="option3" value="F">Formal<font size="1">   (like the last one)</font><br>
  <input type="radio" name="option3" value="I">Informal<br>
</fieldset></td></tr>
<tr><td bgcolor="#ECE5B6"><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000" size="1">
<fieldset><legend><b>Q4: Which US Region Would You Prefer?</b></legend>
  <input type="radio" name="option4" value="NE">Northeast<br>
  <input type="radio" name="option4" value="SE">Southeast<font size="1">   (40th Reunion Was Here)</font><br>
  <input type="radio" name="option4" value="MW">Midwest<br>
  <input type="radio" name="option4" value="SW">Southwest<br>
  <input type="radio" name="option4" value="W">West
</fieldset></td></tr>
<td bgcolor="#EBDDE2"><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000" size="1">
<fieldset><legend><b>Q5: Would You Be Interested In A Cruise?</b></legend>
  <input type="radio" name="option5" value="Y">Yes      
  <input type="radio" name="option5" value="N">No
</fieldset></td></tr>
<tr><td><br>
<center><input type="submit" name="submit" value="Submit My Vote"></center>
</td></tr>
<tr><td><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000" size="1"><br>
<center>
<?php
if(isset($_POST['submit'])) //if vote submitted
{
echo "Thanks for voting.";
}
?>
<br>
<a href="#" onClick="chw=window.open('poll_2_results.php','NewWindow','resizable=no,scrollbars=yes,status=no,left=100,top=120,width=630,height=450'); if (chw != null) chw.focus(); return false" class="sub"><strong>View Results</strong></a></center>
</td></tr>
</table>
<!-- END TABLE 4 -->
</td>
</tr>
</table>
<!-- END TABLE 3 -->
</form>
<?php
}
else {
if(empty($_POST['option1']))
{
echo 'You did not answer question 1. Please select your answer and resubmit.';
exit;
}
if(empty($_POST['option2']))
{
echo 'You did not answer question 2. Please select your answer and resubmit.';
exit;
}
if(empty($_POST['option3']))
{
echo 'You did not answer question 3. Please select your answer and resubmit.';
exit;
}
if(empty($_POST['option4']))
{
echo 'You did not answer question 4. Please select your answer and resubmit.';
exit;
}
if(empty($_POST['option5']))
{
echo 'You did not answer question 5. Please select your answer and resubmit.';
exit;
}
else
{
$opt1=$_POST['option1'];
$opt2=$_POST['option2'];
$opt3=$_POST['option3'];
$opt4=$_POST['option4'];
$opt5=$_POST['option5'];
//GET REMOTE USERNAME AND REFORMAT IT
$name = getenv("REMOTE_USER");
if($name == "webmaster") {
$name = "Webmaster";
} else {
$name = ucwords(substr($name, 1).', '.$name[0]);
}
$no="1"; //use until counter file created
//file
$oldcontent = file_get_contents($file);
$newcontent = '<tr><td>'.$no.'</td><td>'.$name.'</td><td>'.date("M j, Y, g:i A").'</td><td>'.$opt1.'</td><td>'.$opt2.'</td><td>'.$opt3.'</td><td>'.$opt4.'</td><td>'.$opt5.'</td></tr>';
$newcontent.="\n".$oldcontent;
$fp = fopen($file,'w+');
$write = fwrite($fp, $newcontent);
$close = fclose($fp);
//if success
}
?>
<meta http-equiv="refresh" content="0">
<?php
/* }
else {
echo 'Sorry, Your Vote Was Not Saved. Please Try Again.';
}
*/
}
?>
</td></tr>
</table>
<!-- END TABLE 2 -->
</td></tr>
</table>
<!-- END TABLE 1 -->
</body>
</html>