Array for Checkboxes in HTML Forms
I have a question regarding checkboxes.
<form method="post">
I speak the following languages:
<input type="checkbox" name="lang[]" value="en">English<br />
<input type="checkbox" name="lang[]" value="fr">Français<br />
<input type="checkbox" name="lang[]" value="es">Español<br />
</form>
Is it necessary to name the checkboxes lang[] (using an array) or can I give each checkbox a seperate name, like:
<form method="post">
I speak the following languages:
<input type="checkbox" name="lang_en" value="en">English<br />
<input type="checkbox" name="lang_fr" value="fr">Français<br />
<input type="checkbox" name="lang_es" value="es">Español<br />
</form>
Question 1 I believe both works,开发者_如何学Go if so when do you decide which to use?
Question 2 I am using the second method listed above so I can use PHP to detect which of the checkbox is selection by using a code similar to if(isset($_POST['lang_en']))
. If I were to use the first method, is there a quick way to check if a particular checkbox is selected? At the moment the untested solution I can think of involves doing a if(in_array('lang_en', $_POST['lang']))
to check if it exist in $_POST.
Question 3 The main question is this: I am using the second method so I can easily check if a checkbox is selected in PHP. Now I want to add a text link which when clicked will select all the checkboxes. My Javascript is not too good, so I am using a script from http://www.shiningstar.net/articles/articles/javascript/checkboxes.asp but the example script uses an array for the checkbox's names while my PHP code cannot check if the checkboxes are selected with arrays are used for the checkbox's names. How can the javascript code be modified to work without arrays?
Hope I can get this annoying question figured out! Thanks!
EDIT
Javascript:
<!-- Begin
function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}
function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
// End -->
</script>
HTML:
<form action="http://localhost/website/places/search" method="post" accept-charset="utf-8" name="subcategory">
<ul>
<li><input type="checkbox" name="cuisine_American" value="American" /> American</li>
<li><input type="checkbox" name="cuisine_Chinese" value="Chinese" onClick="checkAll(document.subcategory.cuisine)" /> Chinese</li>
<li><input type="checkbox" name="cuisine_Indian" value="Indian" onClick="checkAll(document.subcategory.cuisine)" /> Indian</li>
<li><input type="checkbox" name="cuisine_Japanese" value="Japanese" onClick="checkAll(document.subcategory.cuisine)" /> Japanese</li>
<li><input type="checkbox" name="cuisine_Korean" value="Korean" onClick="checkAll(document.subcategory.cuisine)" /> Korean</li>
<li><input type="checkbox" name="cuisine_Mexican" value="Mexican" onClick="checkAll(document.subcategory.cuisine)" /> Mexican</li>
<li><input type="checkbox" name="cuisine_Middle Eastern" value="Middle Eastern" onClick="checkAll(document.subcategory.cuisine)" /> Middle Eastern</li>
<li><input type="checkbox" name="cuisine_Pakistani" value="Pakistani" onClick="checkAll(document.subcategory.cuisine)" /> Pakistani</li>
<li><input type="checkbox" name="cuisine_Italian" value="Italian" onClick="checkAll(document.subcategory.cuisine)" /> Italian</li>
</ul>
</form>
Answers:
- Either works...depends on whether you want explicit names or an array
- You can also use
array_key_exists()
- Can you post the javascript code you are using into your question? I'll take a further look...
UPDATE:
Here is a jQuery solution for implementing your onclick handlers:
function checkAll(field){
$(':checkbox[name^="cuisine_"]').each(function(index){
this.checked=true;
});
}
function uncheckAll(field){
$(':checkbox[name^="cuisine_"]').each(function(index){
this.checked=false;
});
}
Note that you really don't need to pass in field
, unless you want to generalize this and pass in the string for the startswith pattern (e.g. cuisine_
).
1 . Both work, solution with lang[] is much more flexible. For example if you have really long form with a list of checkboxes in a few categories, eg. languages user speaks, countries he visited, cars he drove, and let's say each list consists of 10 elements, then hard coding every one of them is a pain in the ..., on PHP side you'd have to check every element manually, and with an array you can do this:
if (isset($_POST['lang'] && is_array($_POST['lang'])
{
// let's iterate thru the array
foreach ($_POST['lang'] as $lang)
{
// do some super-magic here
// in your example array elements would be $_POST['lang'] = array('en','fr','es')
}
}
2 . Like said above to check if any of the languages was selected check:
if (in_array('en', $_POST['lang']))
3 . If you are going to use jQuery simply use this snippet:
// selector ATTRIBUTE*=VALUE find all tags wchich ATTRIBUTE string contains VALUE
$('input[name*=lang]').attr('checked', 'checked'); // check
$('input[name*=lang]').removeAttr('checked'); // uncheck
精彩评论