0

I have a comment form and I'm having trouble to empty my post after submitting, well here's my form:

    <?php
if(isset($_POST['comment']))
{
    $fields = array(
        "module" => db_decode($node_delta['tid']) ,
        "delta" => db_decode($node_delta['nid']) ,
        "title" => db_decode($_POST['subject']) ,
        "author" => db_decode($_POST['username']) ,
        "content" => db_decode($_POST['comment']) ,
        "status" => db_decode('pending') ,
        "date_comment" => new Zend_Db_Expr('NOW()') ,
    ) ; 
    db_insert("comment", $fields) ; 

    unset($_POST);

}
            ?>
<form class="form-horizontal" method="post" id="comment" action="" role="form">
        <input type="hidden" id="comment-element-0" value="comment" name="form">
        <input type="hidden" id="comment-element-1" value="insert" name="action">
        <div class="form-group"><label for="comment-element-2" class="control-label col-md-3">
            <span class="required">* </span>Username</label>
            <div class="col-md-6">
                <input type="text" id="comment-element-2" required="" value="" name="username" class="form-control input-md">
            </div>
        </div>
        <div class="form-group"><label for="comment-element-4" class="control-label col-md-3">
            <span class="required">* </span>Subjet</label>
            <div class="col-md-6">
                <input type="text" id="comment-element-4" required="" value="" name="subject" class="form-control input-md">
            </div>
        </div>
        <div class="form-group"><label for="comment-element-5" class="control-label col-md-3">
            <span class="required">* </span>Comment</label>
            <div class="col-md-6">
                <textarea class="form-control input-md" id="comment-element-5" required="" name="comment" rows="5"></textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="form-actions col-md-9 col-md-offset-3 text-right">
                <input type="submit" id="comment-element-6" class="btn btn-primary"  name="" value="Submit">
            </div>
        </div>
</form>

The issue here is if I refresh the value of post still have the same content, why does unset($_POST); doesn't do the trick? Any help with this ? Much appreciated!

user3350731
  • 962
  • 1
  • 10
  • 30

3 Answers3

0

The problem here is that you're refreshing the page, thus reposting the request. To avoid this, you should probably redirect after the current post.

F/e you could do this instead of the unset($_POST);

header("location:".$_SERVER['PHP_SELF']);die;

Make sure there is no content before the header, and the die is needed to make sure the script does not continue. This should load the page anew and remove the possibility to spam your form using F5

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
0

It's something in your browser. Since you haven't loaded your form data back into the HTML part (you give empty values everywhere) it can't be your script that loads back the data.

Also, you shouldn't unset $_POST. It's an internal variable and although it's kinda legal to do it, it's very bad practice - anything else in your (or others) script may rely on its existence.

You need to understand what happens. $_POST is not the one to blame here. Check your browser extensions (form fillers perhaps). There is NO automated mechanism that "silently loads back" your variable into your HTML form; so unless you give something like

<input ...... value="<?=$_POST["foo"]?>">

, your values won't be there. Check the HTML source of the page. You probably won't find the values there either. That's a clear sign that they come back AFTER your page has loaded - so you (as a server-side script) can't do a thing about it.

dkellner
  • 8,726
  • 2
  • 49
  • 47
0

Your browser remember last form's variables. It doesn't use $_POST array. Redirect is good practice in your case. Also you can clean your form by java script, but this is not good idea.