Re: Type of form element

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

 



On Fri, October 28, 2005 10:00 am, Shaun wrote:
> I have some checkboxes on my page which correspond with boolean fields
> in my
> database - actually they are TINYINT's in which I store a 0 or 1 in
> for
> false and true values respectively.
>
> Is it possible to loop through all $_POST values to see if it is a
> checkbox?
> If so then for that element if it is equal to 'on' then change it to 1
> otherwise change it to 0?

No.

The only things you get in POST are:
name (string)
value (string)

PHP does provide the feature (some call it mis-feature) of array
processing on name, so that:

name[index] turns into $name['index']

If you want to identify your checkboxes as checkboxes, you will need
some external, application-specific way to do so.

You could:

#1. Use Hungarian Notation (ugh!) in your checkbox field names, so you
would know that any 'name' that starts with 'ckbx' was a checkbox.

#2. Have an array of known checkbox fields in your PHP
<?php
  $checkboxes = array('spam_me', 'read_terms', 'whatever');
?>
And then you could compare each $_POST index with that array.
Improving performance using $checkboxes with KEYS of the names instead
of values is left as an exercise for the reader :-)


You also need to be aware that HTTP does *NOT* transmit "off" checkboxes.

If the user has 3 checkboxes, and selects only 1, you get *NOTHING* in
$_POST about the other 2 checkboxes.

The very lack of any POST data tells you the checkboxes are "off"

So you will most likely be using isset($_POST['checkbox_name']) rather
than testing for "on"

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
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