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>
		&nbsp <input type="radio" name="option1" value="Y">Yes &nbsp &nbsp &nbsp 
		&nbsp <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>
		&nbsp <input type="radio" name="option2" value="09">2009 &nbsp &nbsp
		&nbsp <input type="radio" name="option2" value="10">2010<br>
		&nbsp <input type="radio" name="option2" value="11">2011 &nbsp &nbsp
		&nbsp <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>
		&nbsp <input type="radio" name="option3" value="F">Formal<font size="1"> &nbsp (like the last one)</font><br>
		&nbsp <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>
		&nbsp <input type="radio" name="option4" value="NE">Northeast<br>
		&nbsp <input type="radio" name="option4" value="SE">Southeast<font size="1"> &nbsp (40th Reunion Was Here)</font><br>
		&nbsp <input type="radio" name="option4" value="MW">Midwest<br>
		&nbsp <input type="radio" name="option4" value="SW">Southwest<br>
		&nbsp <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>
		&nbsp <input type="radio" name="option5" value="Y">Yes &nbsp &nbsp &nbsp 
		&nbsp <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>

    have you tried printing just printing the $_POST hash when form is submited?

    try doing this on your main page?

    print_r($_POST);

    and see if the $_POST has all the elements in there when submitted.

      your code is like this

      if submit is not set
      {

      if submit is set lets print...

      }

      lets use another value,

      $missing_options is not set, then print that welcome message.

        Thanks for your responses.

        slick101: I tried your suggestion and it did give me an array showing all the correct values for each of the options.

        djjjozsi: I don't understand your suggestion of using $missing_options. Can you provide an example?

        I believe one of my problems might be:

        <meta http-equiv="refresh" content="0">

        which takes place toward the end of the file. It might be wipeing out the Thank you when the page refreshes. However, it seems I need this page to refresh in order for the user to click the link to view the results (or to vote again - at least for now).

        By the way, the poll data does correctly write to my "poll_2.txt" file and I have another php page which reads the txt file and displays the results. All that side is working properly.

          when you check the options, lets use an error handle variable:

          if(empty($_POST['option1']))
          {
          echo 'You did not answer question 1. Please select your answer and resubmit.';
          $was_error=1;
          exit;
          }

          and on the very end of your program, lets check that.
          if(empty($was_error))
          print "Thank you...";

          but your code is wrong:

          if(!isset($_POST['submit']))
          {
          ...
          ...
          // in this part if you want to get, your submit value must empty...
          //so in this place your submit value never has a value!!!

              if(isset($_POST['submit'])) //if vote submitted
              {
                echo "Thanks for voting.";      //this part never could run...
           } 

          ...
          ...
          }

          if you want to use <meta http-equiv="refresh" content="0"> ,
          you can use src="form.php?to_do=thank_you_message"

          and on your thank you page lets check if $_GET["to_do"=="thank_you_message"]
          lets print "Thank ouy.";

            Thanks djjjozsi.

            Your suggestion of using the $was_error works great for keeping the values set for the questions answered. However, for the life of me I can't figure out how to get an error message to appear back on the poll form itself (the form disappears and a blank page comes up with the error message).

            Also, assuming all of the poll questions were answered, the poll form completely disappears from the webpage, leaving me again with just a blank page (if I don't use the "meta refresh") which I don't understand. Again, any message I try to place on the poll form itself, gets wiped out by the "refresh".

            Any ideas?

              the method you create the page might has some change.

              put the html head into header.php
              put the values check into check.php
              put the result write part into results_write.php
              put the footer part into footer.php

              put the form into form.php

              put the result reader into results.php
              and then create a file, index.php

              this is schematic version , but its better then the Meta refresh.

              <?php
              session_start();
              
              if ( isset( $_POST ) ) {
              				include( "check.php" );
              
              			if ( $was_error == "" AND $_SESSION["write_into_file"]!=1) {
              							$_SESSION["write_into_file"]="2";
              							include( "results_write.php" );
              							/* after write, set the session into 1*/
              							$_SESSION["write_into_file"]="1";
              							header( "Location: index.php?message=success" );
              			}else {
              							header( "Location: index.php?message=notsuccess" );
              			}
              }else {
              				include( "header.php" );
              
              			if ( empty( $_GET["message"] ) )
              				include( "form.php" );
              			else {
              					switch ( $_GET["message"] )
              					{
              						case "notsuccess":
              						print "Choose one option!";
              						break;
              
              						case "success":
              						print "Thank you for your vote!";
              						break;
              					}
              				}
              
              			include( "footer.php" );
              }
              
              ?>
              
                Write a Reply...