php mutliple fields problem

brendon2020

[H]ard|Gawd
Joined
Dec 15, 2002
Messages
1,541
i have a form with multiple input text fields along with a checkboxes. Problem is not displaying the data but inserting the data into the database. See with checkbox only the values that are checked are submitted. With the text field every value is submitted. So the post data looks like this

[MasterBehaviorPlanFrequency] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
skip a few inputs
[127] => 3
[128] => 3
[129] => 3
)

[MasterBehaviorPlanAddDB] => Array
(
[0] => 61
[1] => 91
[2] => 75
)

How can i match up the data? Thanks in advance.
 
Use isset() to test checkboxes, and prepare database data that way
Code:
if (isset($_REQUEST["checkboxname"]))
{ /* use checked data */ }
else
{ /* use not-checked data */ }
Otherwise, you'll need to elaborate on what your data samples mean, because I can't figure out what's going on (or what's needed) there.
 
see i have a table like this
<table>
<tr><td><input type=text name='MasterBehaviorPlanFrequency[]' size=1 maxlength='1'><input type=checkbox name='MasterBehaviorPlanAddDB'></td><td>Some Behavior Data</td></tr></table>
now this is repeated for each query from the database. So there hundreds of text fields next to checkboxes that are next to some behavior data. I gotta run, but i'll tackle this at home. 5pm time to clock out!!
 
PHP won't see checkboxes that are not checked, not even the form values.

I usually write a hidden form value with a list of checkboxes that are going to be written as checked, then on the processing page decide what has changed.
 
shoeish said:
PHP won't see checkboxes that are not checked, not even the form values.

I usually write a hidden form value with a list of checkboxes that are going to be written as checked, then on the processing page decide what has changed.

could you give me some code of what you do? There anyway i can get php to see uncheck checkboxes, maybe some javascript?
 
Here's a way to use checkboxes:

Html Form:
Code:
Safety Equipment:
<input type="checkbox" name="safetyId[]" value="1">Goggles
<input type="checkbox" name="safetyId[]" value="2">Gloves
<input type="checkbox" name="safetyId[]" value="3">Hard Hat
<input type="checkbox" name="safetyId[]" value="4">Boots
Since the checkboxes will be sent via POST as an array, do a check for an array in the php script
Code:
foreach ($_POST as $key=>$value) {
    if (is_array($value)) {
        //handle it how you want
    }
}
That's not perfect, but it works. The hard part is that unchecked boxes just don't get sent. If you want to use javascript, it would make your code a bit more complicated, but you could have javascript populate a hidden field, as was suggested, for each checkbox on your form.

HTH,

-Treefrog
 
Back
Top