SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unticked checkboxes

    Hello,

    I usually use PHP to figure out the logic of the unticked value of a checkbox.

    However I was wondering if there is a more logical HTML solution? untickedvalue="0" value="1" would be fantastic but I don't see anything like that in the next version of the HTML standard

    I am thinking there might be a good jQuery way todo this.

    Input wanted.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <input type="hidden" name="foo" value="unticked value">
    <input type="checkbox" name="foo" value="ticked value">

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It has been available in HTML as crmalibu has shown since browsers first added support for forms so no alternative way to do it is needed.

    Only the last copy of a field with each field name gets submitted with the form and unchecked checkboxes don't get passed. So if the checkbox is checked it gets passed and the hidden field that pas the same name that preceded it doesn't. If the checkbox is unchecked then the hidden field is passed with the form because there is no following field with the same name to overwrite it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The browser will actually submit all the fields, even if they have duplicate names(but of course unchecked checkboxes aren't sent).
    Code:
    <input type="hidden" name="foo" value="1">
    <input type="hidden" name="foo" value="1">
    <input type="hidden" name="foo" value="1">
    The browser would still send all three. It's the programmers job to recognize this serverside. Most likely your host language will provide you with the last value sent if its parsing the raw http request for variables automatically for you. At least in the browsers I've tested, they send the fields in source order(I don't know if any spec requires them to do this though...). The combination of these two behaviors leave you with the most recent value.

    If you don't want to rely on the browser submitting them in source order, and want a method which won't depend on the host language providing you with the last value received, then adopt a naming scheme which allows you to associate fields together.
    Code:
    <input type="hidden" name="foo_off" value="unticked value">
    <input type="checkbox" name="foo_on" value="ticked value">
    Then you just do a conditonal check in the serverside code.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •