-1

I have a short html form and some php code on the same page so that when the form is submitted, the entries appear on the same page. This code works as far as posting the entered information from the form to the page, but I have 2 problems:

  1. The text box for some reason was only letting me enter 1 character, now it won't let me enter any characters.

  2. Every time I refresh the page to try the form again, the information keeps appending. I only want/need for it to show up once after submission.

    <form method="post" action="">
     <label>Select Out of Office Message
    
     <select name = "selectoutofofficemessage">
      <option value = "N/A">N/A</option>
      <option value = "Vacation">Vacation</option>
      <option value = "Conference">Conference</option>
      <option value = "Meeting">Meeting</option>
      <option value = "Other">Other</option>
     </select>
    
     <label>Custom Out of Office Message
     <input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>
     </label>
    
     <p>
       <input type="submit" name="submit" value="Submit" />
     </p>
    </form>
    
    <?php
     $selectoutofofficemessage = $_POST["selectoutofofficemessage"];
     $customoutofofficemessage = $_POST["customoutofofficemessage"];
     $posts = file_get_contents("posts.txt");
     $posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
     file_put_contents("posts.txt", $posts);
     echo $posts;
    ?>
    
CustomNet
  • 732
  • 3
  • 12
  • 31
user2647160
  • 11
  • 1
  • 2
  • Take care of your (ending-) tags. Eg. the missing tags - or an input tag inside a label ... will not work.... – Petra Nov 05 '13 at 14:45
  • @Petra Input tags inside labels are perfectly valid provided the label tag is properly closed. – GordonM Nov 05 '13 at 14:52

3 Answers3

0

1.First label is incorrect, it doesn't close. Fix it:

<label>Select Out of Office Message
   <select>
       ...
   </select>
</label>

2.Change your PHP code:

<?php

$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";

if(!empty($_POST))
{
    $selectoutofofficemessage = $_POST["selectoutofofficemessage"];
    $customoutofofficemessage = $_POST["customoutofofficemessage"];
    $posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
    file_put_contents("posts.txt", $posts);
}

echo $posts;

Consider each page refresh after a submition causes the last submition.

José Antonio Postigo
  • 2,674
  • 24
  • 17
  • Thanks! Can't believe I didn't see those – user2647160 Nov 05 '13 at 15:05
  • Have a look to this post: http://stackoverflow.com/questions/3518002/how-to-set-default-value-for-html-select-element – José Antonio Postigo Nov 05 '13 at 15:25
0

Put a validation to the Post values

if(isset(selectoutofofficemessage))

{
execute the html code;

}

else {

php code....

} then the value will not be repeated again and again and dont append it just display it....

Ribson
  • 15
  • 6
  • hmm, maybe I am getting the order of the code wrong or I am missing tags? The code you sent me is showing up on the web page and it is still appending the form information... if(isset(selectoutofofficemessage)) {
    MY HTML FORM
    } else { }
    – user2647160 Nov 05 '13 at 15:29
  • Can you help me any further on this? I am very close to having this work. – user2647160 Nov 06 '13 at 14:25
0
<form method="post" action="">

<label>Select Out of Office Message</label>
<select name = "selectoutofofficemessage">

 <option selected="selected" value="">Select a Message</option>

 <option value ="N/A">N/A</option>

 <option value = "Vacation">Vacation</option>

 <option value = "Conference">Conference</option>

 <option value = "Meeting">Meeting</option>

 <option value = "Other">Other</option>

</select>

<label>Custom Out of Office Message</label>

<input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>



<p>

<input type="submit" name="submit" value="Submit" />

</p>

</form>


<?php

$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";

if(!empty($_POST))
{   
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = "$selectoutofofficemessage - $customoutofofficemessage AT  <small><em>". date('h:m A - M-d-Y') ."</em></small>  <br>" . $posts;
file_put_contents("posts.txt", $posts);
}

echo $posts;

?>

<?php
if (!empty($_POST)){
?>
<script type="text/javascript">
    window.location = window.location.href;
</script>
<?php } ?>
user2647160
  • 11
  • 1
  • 2