html and php form field and submission

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 a problem:

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.

Also the “if isset”…“else” code is showing up on my web page and doesn’t seem to be helping my issue.

[code]if(isset(selectoutofofficemessage))
{

<form method="post" action="">

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

    <select name = "selectoutofofficemessage">
        <option selected="selected">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>
    <br />
    <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>

}

else {
<?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;
?>

}[/code]

That is correct.

When you press refresh after submitting the form you are, in effect submitting it again (and again and again etc.)
What you need to do is write some code to stop that happening.
something like
[php]
if($_POST[‘submitted’]) {
$formdone = true;
}
else {
$formdone = false;}
}

if($formdone == true) {
// dont allow any more submissions…
}
else {
// process the form
}[/php]

This is a very rough ‘idea’ how to do it please don’t dump this snippet into a live form without some work adding to it.

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service