开发者

How to clear html form via php?

I want to clear my form with the help of PHP after submitting the form and updating 开发者_如何学Cthe record.


I would recommend you to redirect the user after submit a form. this will clear all POST or GET data.

header('Location: foobar.php');

or you can unset the values after a successful submit.

unset($_POST['fieldname'], $_POST['fieldname2']); 


Just unset the variables that you use to populate your form.


a html form is client side... and PHP runs server side, essentially you are going to have to reset the values of the form using a client side code, on the onsubmit action. The simplest way to do this would be to use javascript to clear the values of all the inputs.


You can unset POST/GET variables and avoid data resend if the page is reloaded:

PHP code: (avoidResend.php):

if(!empty($_POST) OR !empty($_FILES))
{
    $_SESSION['save'] = $_POST ;
    $_SESSION['saveFILES'] = $_FILES ;

    $currentFile = $_SERVER['PHP_SELF'] ;
    if(!empty($_SERVER['QUERY_STRING']))
    {
        $currentFile .= '?' . $_SERVER['QUERY_STRING'] ;
    }

    header('Location: ' . $currentFile);
    exit;
}

if(isset($_SESSION['save']))
{
    $_POST = $_SESSION['save'] ;
    $_FILES = $_SESSION['saveFILES'] ;

    unset($_SESSION['save'], $_SESSION['saveFILES']);
}

include this file at the very top of your page, and after submitting the form then unset($_POST['v1'], $_POST['v2'], $_POST['v3']);

Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜