Re: checkboxes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Nov 29, 2005 at 10:15:36PM -0500, blackwater dev wrote:
> I have a form where I am dynamically building a bunch of checkbox
> inputs.  Then when submitted, I need to update the db.  Problem is, of
> course, if the checkbox is unchecked then the $_POST doesn't have the
> value in the array.  How can I get the value from $_POST even though
> it's unchecked?
> 
> For example, here would be the generated form:
> 
> <input type="checkbox" name="cars[2]" value="1">Car 1
> <input type="checkbox" name="cars[3]" value="1">Car 2
> <input type="checkbox" name="cars[4]" value="1">Car 3
> <input type="checkbox" name="cars[5]" value="1">Car 4
> 
> Then in the update I loop through:
> 
> foreach($_POST[cars] as $id=>$value){
> //update vehicles set value to yes or no
> 
> }

This is a good example on how *not* to trust data via form data
posted.  I'm assuming you have some sort of one to many relation
going on:

  table lot {
    id int,
    lot varchar(255)  -- Lot 1 
  }

  table vehicles {
    id int,
    name varchar(255) -- Car 1, Car 2, etc... 
  }

  table lotvehicle {
    lot_id int,
    vehicle_id int,
  }

The form value of cars[X] where X being the id of vehicle and the
value of 1 being it is selected (although with  the form definition
you just want to know if if cars[X] is defined.).

So we have a form that will eventually need to knnow  what values
should be selected. Potentially data alreadly exists that may have
the same records. There are two options (or perhaps more) to
approach this:

Decide as we go approach and clean up:

  1) Grap the 'vehicles' currently assoicated with the 'lot'
  2) if 'vehicle' is currently selected do nothing
  3) if 'vehicle' is a new item we should add 'vehicle' as long as it
     is a valid 'vehicle;
  4) repeat 2 and 3 until we looked at all the data
  5) figure out which ones should be deleted and delete them.


Forget everything, we'll add what is valid (the lazy way):

  1) delete every associated record to the 'lot'
  2) for each 'vehicle' add 'vehicle' as long as it is a valid
     'vehicle'


HTH,

Curt.
-- 
cat .signature: No such file or directory

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux