Welcome to WebmasterWorld Guest from 54.221.136.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP and arbitrary form checkbox names.

Best way to do it?

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 17, 2003
posts:1947
votes: 0


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

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

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 19, 2003
posts:1001
votes: 0


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.
6:01 pm on Nov 25, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Apr 27, 2003
posts:243
votes: 0


Looks like you missed a ( and a )

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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 17, 2003
posts:1947
votes: 0


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?

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


foreach ($_POST as $name => $value) {
10:47 pm on Nov 25, 2004 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8557
votes: 240


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 17, 2003
posts:1947
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members