On Fri, 2002-08-16 at 19:24, David Yee wrote:
> Hi all.  For a universal form script I'm writing I want to store a 'Y' in a
> table field if a checkbox is checked and an 'N' if it's not.  The problem is
> that when the form is posted if the checkbox is not checked the checkbox
> variable is not passed.  E.g.
> <form method=POST action=submit.php>
> <input type=checkbox name=my_checkbox_var value=Y>
> </form>
> If the checkbox is checked, I get $_POST['my_checkbox_var'] == 'Y', but if
> not $_POST['my_checkbox_var'] is not even set.  So what I've been doing is
> putting the variable names of the checkbox fields into an array, serializing
> it, and then pass the string as a hidden input on the form.  Then in the
> page that handles the POST I unserialize the array to determine if checkbox
> fields were passed and then handle accordingly.  But I'm wondering is there
> a better way (or at least a standard way) of doing this?

I'll assume here that you're dynamically building the table and thus the
checkboxes, which is why you cannot know whether a checkbox exists on
the form or not.

If this is true, I would get a variable $loop, set it to 0, then on
outputting every checkbox, append this value to the checkbox's name.
Then you have predicatable checkbox names.

When you have finished drawing the checkboxes, note in a hidden input
field the $loop value so you know what maximum possible checkbox number

On receiveing the POST, do a for loop, and perform an if
(isset($_POST["checkbox_number_$loop"])) then you know if it's set or

I think that just about covers it. Apologies if I have
misread/misinterpreted what you have asked.

James Green

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

Reply via email to