473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Category checkboxes problem

Newbie here. I have a page that uses a <select> list to assign a
category to an itme, however it is possible for an item to have more
than one category assigned. Right now I have to enter the item and one
of the categories then go back to the edit item page to add a second or
third category.

What I'd like to do is to list the categories ias checkbox items
instead of in a <select> list. The problem is the category keys and
descriptions come from a MySQL atbel, and can be changed. So how do
associate a name and value with each checkbox when I don't know ahead
of time how many checkboxes there will be or what their category keys
will be?

I can do

<INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Category Description
<INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Category Description

Where "123", and "137" in the example are the category keys from the
SQL table, but then how would the page that recieves those values know
which possible variable names to look for?

Any ideas would be appreciated.

--gary

Mar 24 '06 #1
7 1330
On Fri, 24 Mar 2006 08:34:51 -0800, fiziwig wrote:
I can do

<INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Category Description
<INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Category Description

Where "123", and "137" in the example are the category keys from the SQL
table, but then how would the page that recieves those values know which
possible variable names to look for?


foreach ($_POST as $key=>$value) {
if (ereg("cb([0-9]+)", $key, $matches)) {
print "You checked ID $matches[1]";
}
}

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Mar 24 '06 #2
Perfect! Thank you.

As a newbie so far I've only used regexps to validate an email address.
I hadn't really explored their full potential yet.

--gary

Mar 24 '06 #3
> On Fri, 24 Mar 2006 08:34:51 -0800, fiziwig wrote:
I can do

<INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Category Description
<INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Category Description


A different way to do this is

<INPUT TYPE="checkbox" NAME="cb[123]" VALUE="on">Category Description
<INPUT TYPE="checkbox" NAME="cb[137]" VALUE="on">Category Description

Then in you PHP, you can do
<?php
if (isset($_POST['cb']))
foreach $_POST['cb'] as $key=>$dmy)
echo 'You chose catagory ' . $key . '<br>';
?>

This works because only those checkboxes that are actually checked are
sent to your script.

Ken

Mar 24 '06 #4
Message-ID: <11**********************@u72g2000cwu.googlegroups .com> from
Ken Robinson contained the following:
> <INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Category Description
> <INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Category Description

A different way to do this is

<INPUT TYPE="checkbox" NAME="cb[123]" VALUE="on">Category Description
<INPUT TYPE="checkbox" NAME="cb[137]" VALUE="on">Category Description

Then in you PHP, you can do
<?php
if (isset($_POST['cb']))
foreach $_POST['cb'] as $key=>$dmy)
echo 'You chose catagory ' . $key . '<br>';
?>


If he's getting the values from a db it may be slightly easier to do
this:
<INPUT TYPE="checkbox" NAME="cat[]" VALUE="cb123">Category Description
<INPUT TYPE="checkbox" NAME="cat[]" VALUE="cb137">Category Description

<?php
if (isset($_POST['cat'])){
foreach $_POST['cat'] as $value){
echo 'You chose category ' . $value . '<br>';
}
}

Values will only exist if checkboxes are checked.?>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 24 '06 #5
Ken Robinson wrote:
On Fri, 24 Mar 2006 08:34:51 -0800, fiziwig wrote:
> I can do
>
> <INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Category Description
> <INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Category Description
A different way to do this is <INPUT TYPE="checkbox" NAME="cb[123]" VALUE="on">Category Description
<INPUT TYPE="checkbox" NAME="cb[137]" VALUE="on">Category Description Then in you PHP, you can do
<?php
if (isset($_POST['cb']))
foreach $_POST['cb'] as $key=>$dmy)
echo 'You chose catagory ' . $key . '<br>';
?> This works because only those checkboxes that are actually checked are
sent to your script. Ken


or another way is:

-----------begin cut here------------------
<?php
if (isset($_POST['cb']))
{
$cb = implode(",",$_POST['cb']);
echo 'You chose catagory ' . $cb . '<br>';
}
else {
?>
<html><head><title>test </title></head><body>
<h2><center>test </h2></center><p>
<form method=post>
<INPUT TYPE="checkbox" NAME="cb[]" VALUE="123">CB 123<br>
<INPUT TYPE="checkbox" NAME="cb[]" VALUE="133">CB 133<br>
<INPUT TYPE="checkbox" NAME="cb[]" VALUE="143">CB 143<br>
<INPUT TYPE="checkbox" NAME="cb[]" VALUE="153">CB 153<br>
<INPUT TYPE="checkbox" NAME="cb[]" VALUE="167">CB 167<br>
<input type=hidden name=flag value=1><input TYPE=submit value=Submit>
<input TYPE=reset value=Reset></form>
</body></html>
<?php } ?>

-----------end cut here------------------
If you checked the appropriate boxes you get:

You chose catagory 123,133,153
Mar 24 '06 #6
There's more than one way to skin a cat!

These are all interesting, and food for future thought. To tell the
truth I wasn't aware that you could send an arrray as a POST variable.

I tried Andy's solution first, and it works fine. The number of
categories is small, and will probably never grow to more than 15 or
20.

Thanks for all the suggestions.

--gary

Mar 24 '06 #7
On Fri, 24 Mar 2006 09:19:13 -0800, fiziwig wrote:
Perfect! Thank you.

As a newbie so far I've only used regexps to validate an email address. I
hadn't really explored their full potential yet.


No problem mate, more than welcome.
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Mar 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
4
by: Pete | last post by:
Okay, I'm still stuck with this problem. Here's a quick recap/summary :- 1. Page 1:User checks 3 out of 10 checkboxes and submits form to page 2 2. Page 2:Item count shows 3 items. User checks...
3
by: Rich Protzel | last post by:
Hello, So my table contains say 100,000 records, and I need to group the categories in fld1 by the highest count of subcategories. Say fld1 contains categories A, B, C, D, E. All of these...
6
by: terence.parker | last post by:
I currently have the following JS in my header: function checkall(thestate) { var checkboxes=eval("document.forms.EssayList.file_id") for (i=0;i<checkboxes.length;i++)...
5
by: Deborah V. Gardner | last post by:
I would like to use "Yes" and "No" checkboxes on a subform. The problem is that when I click the Yes checkbox on the subform, all of the checkboxes are checked. Currently, I have a field...
1
by: kannadasan | last post by:
Hi all I am using Datagrid where i place checkboxes in one column with some other columns.The purpose is, if i select the checkboxes and clicks the submit buton Email has to go to the selected...
19
by: Brian Kendig | last post by:
I want to have a set of nested UL elements where every LI has a checkbox associated with it, and the checkboxes are all lined up vertically either to the left or the right of the list. In other...
2
by: GISmatters | last post by:
I have unbound checkboxes in a nested gridview to allow multi-selection of "child" rows. For context, the parent gridview rows are for large "reports", the child rows are for various specific files...
1
by: TomLasky | last post by:
Hi everyone, I'm creating a very powerful web store script in ASP. One of the things I am adding is to allow the user to create their own categories and sub categories for items they wish to sell....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.