Forum Moderators: coopster

Message Too Old, No Replies

PHP and arbitrary form checkbox names.

Best way to do it?

         

HughMungus

1:16 am on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a form that is created based on data from a database where the checkbox names are arbitrary (as is the number of checkboxes that might be displayed). I know how to create the form but I'm trying to figure out how the form "submit" page can figure out which names it needs to check as being "on". Is there a better way of doing this than using a numbered looping procedure (e.g., determining the number of checkboxes (X), using numbers as the checkbox names, then doing a loop through X times to see if a checkbox name is checked)?

TIA

Robber

9:08 am on Nov 25, 2004 (gmt 0)

10+ Year Member



Well I guess you could name each checkbox so they start with a common string, eg "chk". Then you could loop through $_POST using foreach and test each post variable to see if it begins with "chk". If it does you can do whatever you need to do.

But Im not sure thats any better than the solution you mentioned already.

mincklerstraat

9:21 am on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This sounds to me pretty much like how I'd do it - except I'd probably do it as an array of names for those checkboxes if they didn't refer to strictly numeric info, so I could keep better track of what they were referring to.

$checkboxarray = 'stone', 'sick_dog', 'ballpoint_pen', 'weasel';
foreach($checkboxarray as $v){
echo "\n".str_replace('_', ' ', $v).' : <input type="checkbox" name="'.$v.'" value="1"';
if(!empty$_GET[$v]) echo ' checked="checked"';
echo ' />';
}

If you get the name from user input, you'll want to 'clean' these variables too.

bobnew32

6:01 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



Looks like you missed a ( and a )

if(!empty($_GET[$v])) echo ' checked="checked"';

HughMungus

6:11 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, Robber. I figured out to do that, too. Thanks for all the replies. I'll keep at it.

Edit: So I guess there's not a way to get a list of what form values are being passed regardless of their name(s), is there?

coopster

10:29 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



foreach ($_POST as $name => $value) {

ergophobe

10:47 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



edit: coopster got there first, but here's a bit more detail

There are many ways...
The first and easiest is simply

print_r($_POST);

Voila! A list of all post vars. Now if you want to actualy be able to do something with those values other than printing them out as a mish mash array listing, you have to do something more. I would approach it like this.

You don't know what the names of the checkboxes are going to be, but you could save yourself a lot of headache (I think) by using an array for your checkbox names. If the name is important you will have to use it as the array index. So your input elements are like this.

<input type="checkbox" name="list[name_from_db]">
Now when you get the post data, you at least know where to look for your list of checkboxes.

foreach($_POST['list'] as $name=>$val)
{
echo "The value of box $name is $val<br>";
}

If I understand what you need, that roughly should do it with some debuigging for typos!

It still doesn't answer your first question about knowing from the submit page which checkboxes need to be checked. In that case, as you pull the data from the DB, you simply check to see whether or not it's true and you flag it as in

$selected = (!empty($val))? ' selected="selected"' : '';

$input_field = '<input type="checkbox" name="arbitrary_value_12ssd3"' . $selected . '">';

Tom

HughMungus

7:46 pm on Nov 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow. Thanks so much, guys; that's perfect. Ergo, thnaks for the extended answer. :D