How to reset specific fields with an extra reset button in one PHP Form

Posted on 2004-10-09
Last Modified: 2008-03-17
Hi everybody
I have a form with tons of fields. I would like to add "Reset Buttons" for specific areas on the form. For example, if the user click reset button under "Item Numbers", it will only reset item numbers, not the other fields. Basically everything is one PHP Form, and when the user click submits, it will submit the whole form. Any suggestions?
Question by:phpisthefuture
  • 6
  • 2
LVL 27

Expert Comment

ID: 12268391
hmm, my first though is to give the html inputs associative array names with a corresponding associative array name for the button. This will be combined with a few other things but is a little difficult to explain in words so il show you an example...

if(isset($_POST['reset'])) {
 $key = key($_POST['reset']);

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Section 1:<br>
<input type="text" name="sec1[element1]" value="<?php if (isset($_POST['sec1'])) echo $_POST['sec1']['element1']; ?>"><br>
<input type="text" name="sec1[element2]" value="<?php if (isset($_POST['sec1'])) echo $_POST['sec1']['element2']; ?>"><br>
<input type="text" name="sec1[element3]" value="<?php if (isset($_POST['sec1'])) echo $_POST['sec1']['element3']; ?>"><br>
<input type="submit" name="reset[sec1]" value="Reset Section"><br>
Section 2:<br>
<input type="text" name="sec2[element1]" value="<?php if (isset($_POST['sec2'])) echo $_POST['sec2']['element1']; ?>"><br>
<input type="text" name="sec2[element2]" value="<?php if (isset($_POST['sec2'])) echo $_POST['sec2']['element2']; ?>"><br>
<input type="text" name="sec2[element3]" value="<?php if (isset($_POST['sec2'])) echo $_POST['sec2']['element3']; ?>"><br>
<input type="submit" name="reset[sec2]" value="Reset Section"><br><br>
<input type="submit" name="submit" value="submit">
LVL 27

Expert Comment

ID: 12268414
I think you might be needing some added notes for this one, so in no particular order off the top of my head...

1) each element in a section has a name that follows this trend


2) each reset button for the corresponding section has a name that follows this trend


3) when you handle the post data you can access each value like this


eg. The first text fields value in my example would be accessed using: $_POST['sec1']['element1']

4) This handles the resetting of a section

if(isset($_POST['reset'])) {
 $key = key($_POST['reset']);

The flow is: if a submit button is pressed, get the assoc key of the button that was pressed, and unset the post array for this item. SO if reset[sec1] is clicked sec1 is the key, and the array for that section is unset from the post array.

5) This is an example from the above of how the values are otherwise maintained

<?php if (isset($_POST['sec2'])) echo $_POST['sec2']['element1']; ?>

If the post item (which is in fact an array) for this item is set then output the value for the matching array value from this section. Obviously if the array is reset using one of the reset buttons prior to this then the post item will not be calssed as being set and hence nothing will be outputted for that section.
LVL 27

Expert Comment

ID: 12268435
6) As an after-thought/tip if at any point you have problems figuring out the correct post reference for a field item add the following after the reset code to get a human friendly output of the data:

echo "<pre>";
echo "</pre>";

For the first section of my example form this would be outputted (if the 3 text boxes contained the values "a", "b" and "c" in the last one)

    [sec1] => Array
            [element1] => a
            [element2] => b
            [element3] => c

so you can see to access the value of the second item in the POST array for this section you would need $_POST['sec1']['element2']

7) This should work find for the majority of the inputs you might need to do some tweaking for certain items such as checkboxes and some select boxes but i am hoping you will have enough knowledge to interpret the code and see how this would be done.

Heres an example with a select box to help you on your way if needed (extending the original example - presume it would be in section 1):

<select name="sec1[element4]">
<option <?php if (isset($_POST['sec1']) && $_POST['sec1']['element4'] == "one") echo "selected"; ?>>one</option>
<option <?php if (isset($_POST['sec1']) && $_POST['sec1']['element4'] == "two") echo "selected"; ?>>two</option>
<option <?php if (isset($_POST['sec1']) && $_POST['sec1']['element4'] == "three") echo "selected"; ?>>three</option>

The php syntax is basically if the post array is set for this sections name... and the value stored in the array item for this element equals the value of the option then output selected.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 27

Accepted Solution

Diablo84 earned 500 total points
ID: 12268508
There is of course the "manual" way of doing this which is far less efficient but will mean you don't have to rename your input fields. Take note that given a choice i would recommend the above method instead of this because this way is far more inconvienient in the long run.

Basically you have a reset button for each section but now they would have a normal name (a normal unique name) eg.

<input type="submit" name="reset_one" value="Reset Section">


<input type="submit" name="reset_two" value="Reset Section">


Then to process it you would do something like this

if (count($_POST) > 0) { //the post array is not empty and so has been submitted

 if (isset($_POST['reset_one'])) {
 if (isset($_POST['reset_two'])) {


you would then maintain the values in the fields roughly the same as before, eg:

<input type="text" name="someelement" value="<?php if (isset($_POST['someelement'])) echo $_POST['someelement']; ?>">

so as you can see basically you have to manually specify exactly what post fields to unset based upon which submit button was clicked... im sure you can appreciate that this is a pretty bad method so my advice is treat this post as a "for your information" rather then a solution to use. Just imagine if you expanded your fields... what a nightmare it would be maing modifications.

Apologies for the extensive length and quantities of posts in this thread, theres quite a lot of information to get across so you can get the full picture.

Good Luck
LVL 27

Expert Comment

ID: 12268556
I wont be able to check back before the morning now so hopefully there is enough information here, if you need to know anything else i will get back to you then.

Author Comment

ID: 12272223
Hi Thanks Diablo
Great information. I didn't have a chance yesterday to check back. I will play with it today, and maybe ask you if I can't get it work.
Thanks again

Author Comment

ID: 12272227
Just a question, how can I give extra points? I tried to give extra points for this question, but I couldn't. Please let me know.
LVL 27

Expert Comment

ID: 12272290
No Problem.

Don't worry about extra points, the limit is 500 per question (and that includes extra "points for... " questions as well).

Best Wishes

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question