Page 1 of 1

Clearing Contents of $_POST variables

Posted: Mon Jul 25, 2005 12:55 pm
by bwv2
I'm having a bit of trouble with one of my pages. I want to have a page with a bunch of form fields to be filled out. Each field takes information about the customer's appliances that they own. The forms take wattage, quantity owned, etc. However, they may have more appliances than the number of fields available on one page. Therefore, I have included a checkbox at the bottom of the page that can be checked if they want to be presented with another page of fields. If the box is checked, then they hit "submit", then they are returned to the same page to be filled out again.

The data is taken from the fields and stored in variables when the submit button is hit, like this:

Code: Select all

$_SESSION['watts1']=$_POST['field_1_watts'];
$_SESSION['watts2']=$_POST['field_2_watts'];
and so on and so on. However, when I return the same form, there is no guarantee that they will fill out all of the fields the 2nd time around, as they may run out of appliances half way through. Thus, if they hit "submit" after only filling out half of page 2, all of the $_POST fields from the first time through will not be replaced by new values. i.e. they fill out 20 fields on page 1, then they are delivered page 2 of fields and they only fill out 10. Now the last 10 $_POST values will still be the same values from page 1, while the first 10 $_POST values will be replaced by the new entries.

This poses a problem, so what I want to do is have the user fill out page one, then if they want to have page 2 the $_POST values that they just entered will be tabulated, then reset. This way the field slots can be returned repeatedly and then after the values from each are used they can be reset in case new values will be input into the same field name on the next page.

I hope I'm making sense. The bottom line is this: can I reset $_POST values? Can I use something like:

Code: Select all

unset($_POST['watts1'])
Any ideas? Thanks, sorry if I'm confusing.

Posted: Mon Jul 25, 2005 1:06 pm
by John Cartwright
$_POST values are writtable, so yes you can unset the values, or simply $_POST['var'] = array(); to delete the values.

Posted: Mon Jul 25, 2005 3:35 pm
by shiznatix
but the post array will still exist so if you do

Code: Select all

if (!isset($_POST))
{
//display form
}
else
{
//process
}
it will always go to the process part