开发者

PHP array question

开发者 https://www.devze.com 2023-01-12 21:21 出处:网络
Is it alright if I put the cat[] array value in the for, name and id attributes. code. <label for=\"cat[]\">\' . $cat[\'category\'] . \'</label>

Is it alright if I put the cat[] array value in the for, name and id attributes.

code.

<label for="cat[]">' . $cat['category'] . '</label>
&开发者_StackOverflowlt;input type="checkbox" name="cat[]" id="cat[]" value="' . $cat['id'] . '" />


Short Answer: No.

Long Answer

No because:

  • Square brackets are invalid characters for both the for and id attributes.
  • Using square brackets in the name attribute signals to php to create an array. This implies you have multiple form elements with the name cat[]. However, the id attribute must be unique. Therefore, you can't have the id and name equal. If you don't have a decent unique id for each checkbox then just increment cat. So the first checkbox would have an id of cat1, the second an id of cat2 and so on.
  • If you created multiple checkboxes with the same id attribute, your label elements would not know which inputs to point to as the for attribute cannot uniquely identify an input. This would create unpredictable behaviour and make it pointless to have a for attribute in the first place.


No, just name (passed to server side), ID must be unique with no special characters and cannot start with a number.


From w3C

character "[" is not allowed in the value of attribute "for"

character "[" is not allowed in the value of attribute "id"

Try it out validator

0

精彩评论

暂无评论...
验证码 换一张
取 消