Welcome to WebmasterWorld Guest from 50.16.78.128

Forum Moderators: incrediBILL

Message Too Old, No Replies

Naming Checkboxes in Forms

What is the best practice for naming multiple checkboxes in an html form?

   
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
3:23 pm on Aug 3, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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?"
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
3:49 pm on Aug 3, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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']);
?>
5:38 pm on Aug 3, 2011 (gmt 0)



Cool, thank you!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month