最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Array for Checkboxes in HTML Forms - Stack Overflow

programmeradmin6浏览0评论

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, 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 .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>

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, 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/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>
Share Improve this question edited Jun 20, 2011 at 20:18 AJ. 28.2k19 gold badges87 silver badges97 bronze badges asked Jun 20, 2011 at 13:54 NyxynyxNyxynyx 63.7k163 gold badges506 silver badges855 bronze badges 2
  • 1 Additional option - <input type='checkbox' name='lang[en]'/>. That's what I use when there is a need for dynamic list of checkboxes. When the list is static (i.e., hard-coded), I use name='lang_en' option. And I never use value attribute with checkboxes. – binaryLV Commented Jun 20, 2011 at 13:58
  • wow thats so cool, I have to try this out, thanks! – Nyxynyx Commented Jun 20, 2011 at 19:59
Add a ment  | 

2 Answers 2

Reset to default 6

Answers:

  1. Either works...depends on whether you want explicit names or an array
  2. You can also use array_key_exists()
  3. 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
发布评论

评论列表(0)

  1. 暂无评论