Jugal Patel
Jugal Patel

Reputation: 71

How to clear html form via php?

I want to clear my form with the help of PHP after submitting the form and updating the record.

Upvotes: 1

Views: 16866

Answers (4)

Waiyl Karim
Waiyl Karim

Reputation: 2950

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!

Upvotes: 0

Daniel
Daniel

Reputation: 349

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

Upvotes: 1

Flask
Flask

Reputation: 4996

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']); 

Upvotes: 3

samccone
samccone

Reputation: 10926

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.

Upvotes: 0

Related Questions