You are here: Re: [PHP] Type of form element « PHP « IT news, forums, messages
Re: [PHP] Type of form element

Posted by "Richard Lynch" on 10/29/05 22:59

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация