Solved

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

Posted on 2004-10-09
8
437 Views
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?
0
Comment
Question by:phpisthefuture
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 27

Expert Comment

by:Diablo84
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...


<?php
if(isset($_POST['reset'])) {
 $key = key($_POST['reset']);
 unset($_POST[$key]);
}
?>

<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">
</form>
0
 
LVL 27

Expert Comment

by:Diablo84
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

name_of_section[name_of_input_field]

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

reset[name_of_section]

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

$_POST['name_of_section']['name_of_input_field'];

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']);
 unset($_POST[$key]);
}

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.
0
 
LVL 27

Expert Comment

by:Diablo84
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>";
print_r($_POST);
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>
</select>

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.
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 27

Accepted Solution

by:
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">

or

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

etc

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'])) {
  unset($_POST['inputname'];
  unset($_POST['inputname2'];
  unset($_POST['inputname3'];
  }
 if (isset($_POST['reset_two'])) {
  unset($_POST['anotherinputname'];
  unset($_POST['anotherinputname2'];
  unset($_POST['anotherinputname3'];
  }

}

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
0
 
LVL 27

Expert Comment

by:Diablo84
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.
0
 
LVL 2

Author Comment

by:phpisthefuture
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
0
 
LVL 2

Author Comment

by:phpisthefuture
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.
Thanks
0
 
LVL 27

Expert Comment

by:Diablo84
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
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

687 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