www.webdeveloper.com
Results 1 to 7 of 7

Thread: Posting an UNCHECKED check box.

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    73

    resolved Posting an UNCHECKED check box.

    Hi everyone!
    It looks like many programmers need to send UNCHECKED checkboxes.
    I found an "elegant" way to do just that!

    1. check for checked value true or false:

    <input type="checkbox" id="q11" value="" name="too_busy" onClick="yesno(this.id);">

    2. if false, set the INPUT VALUE to "no" or whatever...
    3. FORCE the check box to BE CHECKED
    here is the JavaScript:

    function yesno(q) {
    qc = document.getElementById(q).checked
    qn = document.getElementById(q).name
    if(!qc) {document.getElementById(q).checked = 'true'; document.getElementById(q).value = "NO"} else {document.getElementById(q).value = "Yes" }
    }

    4. SUBMIT it by javascript: document.form.submit();

    In PHP $_POST
    will have a YES for a checked box
    and a NO for an UNCHECKED box.

    I hope it helps anyone.
    GoodLuck!

  2. #2
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    I dont get why the PHP programmer would need to have an unchecked checkbox posted to them.

    Why not just put something like

    Code:
    if(isset($_POST['checkbox'])&&$_POST['checkbox']!="yes"){
            //other code here
    }

  3. #3
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    Quote Originally Posted by ryanlund View Post
    I dont get why the PHP programmer would need to have an unchecked checkbox posted to them.

    Why not just put something like

    Code:
    if(isset($_POST['checkbox'])&&$_POST['checkbox']!="yes"){
            //other code here
    }

    LOL what was i thinking when i wrote that =s

    update:

    Code:
    if(!isset($_POST['checkbox'])||$_POST['checkbox']!="yes"){
            //other code here
    }

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    Using javascript is quite ugly, just add a hidden field with the same name and a value of 0, that way it works for everyone.

  5. #5
    Join Date
    Sep 2005
    Posts
    73
    Hi.
    Why do I need to post unchecked box? simple.

    I have to create a .csv file to be opened by Excel.
    so every post MUST be the same number of parameter (columns)
    or the columns will "move" every line....

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    Well, you still don't need an unchecked checkbox, you can just check if the value isset(). However that said it can certainly make it simpler to have one, especially where you are dealing with checkbox arrays:

    HTML Code:
    <input type="hidden" name="checkbox[1]" value="0" />
    <input type="checkbox" name="checkbox[1]" value="1" />

  7. #7
    Join Date
    Apr 2007
    Posts
    1,664
    You can assume, when the form is submitted, a missing checkbox key from the POST array is an unchecked box. If you rely on jS to provide a value you're more likely to have a screwed up CSV then using PHP.

    Good that you're trying to solve problems by thinking laterally though
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles