3

Well, I guess the title says it all. I'm looking for a way to reset all the fields within a form. I've tried some of the following:

<input type="reset" value="Clear all fields">

And

<button type="reset">Clear all fields</button>

Yet, none of this seems to be working.

This is a stripped version of my form.

    <form id="form2" action="mainframe.php?paso=21" method="POST">
       <input type="reset" value="Reset">
       <button type="reset" form="form2">Reset</button>
       <?php while($row=$resultado->fetch_assoc()){ ?>
       <p>Number of <?php echo $row['name']; ?>
       <input type="text" name="saldo[<?php echo $row['id']; ?>]" value="<?php echo $row['saldo']; ?>" maxlength="30" />
       <?php } ?>
    </form>
    <button type="submit" form="form2">Send</button>

Edit: Apparently the reset button will replace the values of all inputs with the values they had on page load. The button would clear all fields and leave them blank only if the input's value property aren't declared or are null on page load.

2
  • Check your browser for script errors when you click the reset button Commented Dec 5, 2014 at 6:06
  • Thanks for the edit - simple but helpful point about the resetting to default values! Commented Nov 15, 2016 at 12:41

3 Answers 3

2

Guess what. It actually DOES work. I didn't change anything at all. I promise:

<form id="form2" action="mainframe.php?paso=21" method="POST">
       <input type="reset" value="Reset">
       <button type="reset" form="form2">Reset</button>
       <?php while($row=$resultado->fetch_assoc()){ ?>
       <p>Number of <?php echo $row['name']; ?>
       <input type="text" name="saldo[<?php echo $row['id']; ?>]" value="<?php echo $row['saldo']; ?>" maxlength="30" />
       <?php } ?>
    </form>
    <button type="submit" form="form2">Send</button>

If it's not working on your site, then you may have another syntax error.

Sign up to request clarification or add additional context in comments.

2 Comments

So weird, It doesnt seem to work on my computer. I've tried Chrome and Firefox. Also, I've dissabled all extensions. At least now I know the problem is not in the code
Hmmmm. Give us the NOT stripped down version of your form.
2

get rid of what u echo to the input value and it should work...

2 Comments

Please add suggestions as comment and only complete detailed answers here
Indeed, that is the cause of the problem. I was expecting the button to actually "Clear" the fields (meaning leaving it blank) but instead the reset button seems to place the value of all inputs when the page was loaded. Thank you so much for the answers Matmot and God is good :)
1

The previous answers are correct. The reset button resets the form's inputs to their initial values, not to blank.

In my case, I wanted to blank all of my inputs. I thought about writing some JavaScript that sets all the inputs to "", but I decided that just reloading the page would be easier. In my case, reloading the page blanks the form.

Try this:

<input type="button" onclick="window.location='http://www.yourwebsite.com/form.php'" value="Reset" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.