Clear the form field after successful submission of php form

how to clear textbox value after submit in php
how to clear form fields after submit in html
do not clear form after submit php
reset form after submit react
jquery reset form after submit
clear submit php
django clear form after submit
don't reset form after submit javascript

I am making a simple Form the problem i am facing is when i submit the form values still remains in the field. and I want to clear it after SUCCESSFUL submission. Please help.

here is my code for the form..

<label class="w">Plan :</label>
<select autofocus="" name="plan" required="required">
    <option value="">Select One</option>
    <option value="FREE Account">FREE Account</option>
    <option value="Premium Account Monthly">Premium Account Monthly</option>
    <option value="Premium Account Yearly">Premium Account Yearly</option>
<label class="w">First Name :</label><input name="firstname" type="text" placeholder="First Name" required="required" value="<?php echo $_POST['firstname'];?>"><br>
<label class="w">Last Name :</label><input name="lastname" type="text" placeholder="Last Name" required="required" value="<?php echo $_POST['lastname'];?>"><br>
<label class="w">E-mail ID :</label><input name="email" type="email" placeholder="Enter Email" required="required" value="<?php echo $_POST['email'];?>"><br>
<label class="w">Password :</label><input name="password" type="password" placeholder="********" required="required" value="<?php echo $_POST['password'];?>"><br>
<label class="w">Re-Enter Password :</label><input name="confirmpassword" type="password" placeholder="********" required="required" value="<?php echo $_POST['confirmpassword'];?>"><br>
<label class="w">Street Address 1 :</label><input name="strtadd1" type="text" placeholder="street address first" required="required" value="<?php echo $_POST['strtadd1'];?>"><br>
<label class="w">Street Address 2 :</label><input name="strtadd2" type="text" placeholder="street address second"  value="<?php echo $_POST['strtadd2'];?>"><br>
<label class="w">City :</label><input name="city" type="text" placeholder="City" required="required" value="<?php echo $_POST['firstname'];?>"><br>
<label class="w">Country :</label><select autofocus="" id="a1_txtBox1" name="country" required="required" placeholder="select one" value="<?php echo $_POST['country'];?>">

Any help would be appriciated

They remain in the fields because you are explicitly telling PHP to fill the form with the submitted data.

<input name="firstname" type="text" placeholder="First Name" required="required" 
value="<?php echo $_POST['firstname'];?>">
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ HERE

Just remove this, or if you want a condition to not do so make a if statement to that echo or just cleanup the $_POST fields.

$_POST = array(); // lets pretend nothing was posted

Or, if successful, redirect the user to another page:

header("Location: success.html");
exit; // Location header is set, pointless to send HTML, stop the script

Which by the way is the prefered method. If you keep the user in a page that was reached through a POST method, if he refreshes the page the form will be submitted again.

Clearing Form after submit - PHP, What is the normal behaviour of the form after submission. If not how would you clear the form using only PHP? In other words, you can have just plain form-​controls such as text input boxes, textareas, etc. from the form to a 'success' message on a different page if the form is submitted successfully. In the code append few lines of code in this manner that clears the information entered in the text fields if the successfull form submission is made,remember if successful form submission is made.

You can use .reset() on your form.


How to clear form after successful submit using php, You need to learn about PRG. Post/Redirect/Get. By the way, PHP_SELF is vulnerable to an XSS Attack. When using PHP in the values for each input field, which is meant to retain user input in case of an error, the values remain displayed even after a successful submission of the form. How would you reset the form after successful submission to clear all fields? Thanks. Michael

The POST data which holds the submitted form data is being echoed in the form, eg:

<input name="firstname" type="text" placeholder="First Name" required="required" 
value="<?php echo $_POST['firstname'];?>"  

Either clear the POST data once you have done with the form - ie all inputs were ok and you have actioned whatever your result from a form is. Or, once you have determined the form is ok and have actioned whatever you action from the form, redirect the user to a new page to say "all done, thanks" etc.

header('Location: thanks.php');

This stops the POST data being present, it's know as "Post/Redirect/Get":

The Post/Redirect/Get (PRG) method and using another page also ensures that if users click browser refresh, or back button having navigated somewhere else, your form is not submitted again. This means if your form inserts into a database, or emails someone (etc), without the PRG method the values will (likely) be inserted/emailed every time they click refresh or revisit the page using their history/back button.

[RESOLVED] Clearing form fields once data is submitted , I have looked online to see if there is a way to clear form fields but it involves $​errorMsg =''; if (isset($_POST['submit'])) { //grab the form data $curnum = 0; //starts a count <input name="vtitle" type="text" class="formw" value="<?php if Any suggestions on clearing form fields after all data is received and  Resetting symfony form when same page handles the submission. In Bazyl's method creating another form after unsetting current form may be unnecessary task. I suggest to redirect to same page because symfony documentation (handling-form-submissions) also shows an example which redirects to another controller.

I was facing this similiar problem and did not want to use header() to redirect to another page.


Use $_POST = array(); to reset the $_POST array at the top of the form, along with the code used to process the form.

The error or success messages can be conditionally added after the form. Hope this helps :)

How to clear the form filled data after submitting the form in HTML , p> <form id="myForm"> First name: <input type="text" name="fname"><br> Last name: How can we submit an HTML form without a submit button? function and unshift in PHP to produce tables from data received from an HTML form? However, once the data is submitted, with no errors, I would like the form to clear once the submit button is clicked. I have looked online to see if there is a way to clear form fields but it involves javascript or jquery and the explanations were unclear.

Put the onClick function in the button submit:

<input type="text" id="firstname">
<input type="text" id="lastname">
<input type="submit" value="Submit" onClick="clearform();" />

In the <head>, define the function clearform(), and set the textbox value to "":

function clearform()
    document.getElementById("firstname").value=""; //don't forget to set the textbox id

This way the textbox will be cleared when you click the submit button.

Basic Form Handling in PHP < PHP, Basic instructions for setting up a form handler in PHP to verify user textarea></​label></p> <p><input type="submit" name="sendfeedback" value="Send That's because we are using the "redirect-after-POST" technique as How would you reset the form after successful submission to clear all fields? Use page reloading to clear the form filled data in the html form. Page reload will work most probably and if this doesn't work. Use reload () This function automatically reloads the data and wil clear the form filled and make the fields empty again.

Using JavaScript to reset or clear a form, After clearing the form, if I fill the form out again and click on the Submit button, the form’s value for $_POST['type'] is cleared also. How to retain that setting? I  I'd like to simply have the two input fields in my page to clear when I click submit (&& enter - unless its a separate event, that's for another thread), considering I always press enter as opposed to clicking - this is a site just meant for my use. I have of course a input tag, type submit, and its working fine.

Retaining Values In A Form Following PHP Postback And Clearing , Retaining Values In A Form Following PHP Postback And Clearing Form Values After Successful PHP Form I was just wondering, if I have PHP validation on a field of a large form, say for example the if (isset( $_POST [ 'submit' ])) { So how do you clear form values in PHP if the form was properly  The form targets the iframe, so I pass data to it, by entering text into the field, and hitting enter, or clicking submit… data is passed, BUT for some reason, the text field does not clear… I’ve tried doing onSubmit with a function to clear data, but that is before the data gets passed,

HTML DOM Form reset() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  I need to reset or clear all fields in a form, after the form has been submitted. Any suggestions for an easy way to do that? Here is the code:

  • Removing the the value render is not really a good idea in case the submission does not go through.
  • Well if its a good idea or not it will only depend on how he is coding his site.
  • @Havenard: when i remove the value="<?php echo $_post['firstname'];?>". in this case when i submit form my form's field got empty.but if there is any validation like "email alreay exist" if this validation works then i have to fill the form again while i dont want that.
  • Refresh the answer, I've given plenty of other solutions.
  • OP is not using JavaScript. This answer is not congruent with the question.
  • I don't want to send any other page i want to show thank you message on the same page
  • NP - In which case before you display your form, clear the $_POST array with $_POST = array();
  • how can i make this?? please tell me
  • In this case if validation works then also form would be clear. i want if any validation works form shouldn't be clear . form will be clear if and only if the data entered is correct and got stored in database .