SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    question on input type checkboxes

    When you've specifed a checkbox to have an attribute of checked with the value checked, should you have a value attribute in it to?

    example:
    Code HTML4Strict:
    <form action="terms.php" method="post">
     
    <input type="checkbox" name="chkAcceptTerms" value="Accepted terms and conditions" checked="checked" />
     
    <input type="submit" value="Accept" />
     
    </form>

    What i'm thinking is what if the person unchecked the box and submited the form? what value will be sent? What is the right way to approach this?

    Thanking you.
    Cheers,
    WebonCloud
    weboncloud@gmail.com

  2. #2
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the checkbox is unchecked, it will not be set in $_POST variable.

    The value attached to the checkbox input is useful if you had a list of options that you wish to present to the user. Just an example: you presented the user with a page, containing a list of their private messages, with option to mark the messages as read and/or unread. So, you'd provide a checkbox next to each message containing the message ID. When the user choose their operation (marking it as read and/or unread), you'd traverse through each checkbox to do the selected operation on the selected messages.

    Please visit W3 site and read on the Form section to treat yourself some reading about successful control.
    ~elantorh

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by weboncloud View Post
    When you've specifed a checkbox to have an attribute of checked with the value checked, should you have a value attribute in it to?
    Yes. The value attribute is required for checkboxes and radio buttons.

    Quote Originally Posted by weboncloud View Post
    What i'm thinking is what if the person unchecked the box and submited the form? what value will be sent?
    None. The field will not be sent in the request if the checkbox is unchecked.
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you
    Cheers,
    WebonCloud
    weboncloud@gmail.com

  5. #5
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird thing with this for me also, while i tried check a few times in PHP whether a box was checked I would get "no" or "yes". Though this was without a set value too.

    So In PHP I do this for now:
    PHP Code:
    if (isset($_POST['boxname'])) {
        
    $boxname $_POST['boxname'];
        } 
        else {
        
    $boxname 0;


  6. #6
    SitePoint Enthusiast antitoxic's Avatar
    Join Date
    Apr 2008
    Location
    London, UK
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JREAM View Post
    Weird thing with this for me also, while i tried check a few times in PHP whether a box was checked I would get "no" or "yes"
    Could this be only when sent via some javascript ?

    //A bit offtopic. But having <button> element in form and submitting it in some browsers (don't remember which) will send the text between the tags, other the value attribute, and sometimes in IE - none.


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
  •