homepage Welcome to WebmasterWorld Guest from 54.161.247.22
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

    
Naming Checkboxes in Forms
What is the best practice for naming multiple checkboxes in an html form?
rwilson




msg:4347197
 2:56 pm on Aug 3, 2011 (gmt 0)

What is the best way to name multiple groups of checkboxes in an html form?

I have a list of companies that I want the user to click to say which ones they are a vendor for. I've put the code below for how I am currently doing this, I'm just curious if there is a better cleaner way to do it. Any help would be greatly appreciated!


<input type="checkbox" name="CURRENT-vendor-companyA" id="companyA" value="yes" />
<label for="companyA">Company A</label>

<input type="checkbox" name="CURRENT-vendor-companyB" id="companyB" value="yes" />
<label for="companyB">Company B</label>


The email response looks like:
CURRENT-vendor-companyA: yes
CURRENT-vendor-companyB: yes

 

Fotiman




msg:4347213
 3:23 pm on Aug 3, 2011 (gmt 0)

I would probably do it more like this:

<input type="checkbox" name="CURRENT-vendor" id="companyA" value="companyA" />
<label for="companyA">Company A</label>

<input type="checkbox" name="CURRENT-vendor" id="companyB" value="companyB" />
<label for="companyB">Company B</label>


You are asking a single question:
"Who are you a current vendor for?"
The answer may contain multiple values. I think this is probably easier to maintain than asking multiple questions:
"Are you a current vendor for CompanyA?"
"Are you a current vendor for CompanyB?"

rwilson




msg:4347217
 3:30 pm on Aug 3, 2011 (gmt 0)

I've tried that but when I submit it I only see the question once and it fills in the last given answer

CURRENT-vendor: companyB

this shows up if both are selected. Is it because of the name attribute being the same? I'm using a basic php mail function

Fotiman




msg:4347229
 3:49 pm on Aug 3, 2011 (gmt 0)

You need to give the input a name ending in []. As in:


<input type="checkbox" name="CURRENT-vendor[]" id="companyA" value="companyA" />
<label for="companyA">Company A</label>

<input type="checkbox" name="CURRENT-vendor[]" id="companyB" value="companyB" />
<label for="companyB">Company B</label>

Then you will be able to treat the result as an array in PHP. Like so:

<?php
print_r($_POST['CURRENT-vendor']);
?>

rwilson




msg:4347270
 5:38 pm on Aug 3, 2011 (gmt 0)

Cool, thank you!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved