Checkboxes: Setting POST values if unchecked

Discussion in 'Javascript' started by Robert S, Apr 23, 2007.

  1. Robert S

    Robert S Guest

    I have a form with a checkbox:

    <form method="post" action="test.php">
    <input type="checkbox" name="foo" checked>
    <input type="submit">
    </form>

    If the checkbox is checked, $_POST['foo'] gets assigned the value
    "on" (using php as the second file)

    But if the checkbox is unchecked, $_POST['foo'] is not assigned.

    Is there a simple way that I can set an arbitrary value to this
    (eg."off"), if the checkbox is not checked? An onClick method for the
    submit button would be a good start.

    I'm sure this must be a common problem, but I can't find a simple,
    relatively generic solution on the web.
     
    Robert S, Apr 23, 2007
    #1
    1. Advertising

  2. Robert S

    RobG Guest

    Robert S wrote:
    > I have a form with a checkbox:
    >
    > <form method="post" action="test.php">
    > <input type="checkbox" name="foo" checked>
    > <input type="submit">
    > </form>
    >
    > If the checkbox is checked, $_POST['foo'] gets assigned the value
    > "on" (using php as the second file)
    >
    > But if the checkbox is unchecked, $_POST['foo'] is not assigned.


    You need to read the HTML specification again. Any control without a
    value need not be submitted at all. An unchecked checkbox need not be
    submitted either, so even if the checkbox "foo" is checked, it may not
    be submitted because it has no value.

    If it isn't checkeed, it very likely will not be submitted by any browser.


    > Is there a simple way that I can set an arbitrary value to this
    > (eg."off"), if the checkbox is not checked? An onClick method for the
    > submit button would be a good start.


    No, it would be a bad start. You can't depend on client-side scripting
    doing anything.


    > I'm sure this must be a common problem, but I can't find a simple,
    > relatively generic solution on the web.


    If your server gets no value for "foo", it wasn't checked. No client
    side scripting, nothing fancy, just plain server-side logic.


    --
    Rob
     
    RobG, Apr 23, 2007
    #2
    1. Advertising

  3. Robert S

    naph Guest

    On Apr 23, 9:16 pm, Robert S <>
    wrote:
    > I have a form with a checkbox:
    >
    > <form method="post" action="test.php">
    > <input type="checkbox" name="foo" checked>
    > <input type="submit">
    > </form>
    >
    > If the checkbox is checked, $_POST['foo'] gets assigned the value
    > "on" (using php as the second file)
    >
    > But if the checkbox is unchecked, $_POST['foo'] is not assigned.
    >
    > Is there a simple way that I can set an arbitrary value to this
    > (eg."off"), if the checkbox is not checked? An onClick method for the
    > submit button would be a good start.
    >
    > I'm sure this must be a common problem, but I can't find a simple,
    > relatively generic solution on the web.


    the first question you need to ask is do you really need "off"? isn't
    an empty string as good as off (maybe ur particular problem really
    does need an "off", but it's something worth thinking about if you
    haven't already).

    rod.
     
    naph, Apr 23, 2007
    #3
  4. Robert S

    -Lost Guest

    RobG wrote:
    > Robert S wrote:
    >> I have a form with a checkbox:
    >>
    >> <form method="post" action="test.php">
    >> <input type="checkbox" name="foo" checked>
    >> <input type="submit">
    >> </form>
    >>
    >> If the checkbox is checked, $_POST['foo'] gets assigned the value
    >> "on" (using php as the second file)
    >>
    >> But if the checkbox is unchecked, $_POST['foo'] is not assigned.

    >
    > You need to read the HTML specification again. Any control without a
    > value need not be submitted at all. An unchecked checkbox need not be
    > submitted either, so even if the checkbox "foo" is checked, it may not
    > be submitted because it has no value.
    >
    > If it isn't checkeed, it very likely will not be submitted by any browser.
    >
    >
    >> Is there a simple way that I can set an arbitrary value to this
    >> (eg."off"), if the checkbox is not checked? An onClick method for the
    >> submit button would be a good start.

    >
    > No, it would be a bad start. You can't depend on client-side scripting
    > doing anything.
    >
    >
    >> I'm sure this must be a common problem, but I can't find a simple,
    >> relatively generic solution on the web.

    >
    > If your server gets no value for "foo", it wasn't checked. No client
    > side scripting, nothing fancy, just plain server-side logic.


    Yep. RobG is, as usual, right. That is why functions like isset()
    exist in PHP for example.

    if (!isset($_POST['foo'])) { print 'It prints *this* text, because
    isset(), in this example, returns a Boolean FALSE.'; }

    This was a basic PHP question.

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.
     
    -Lost, Apr 23, 2007
    #4
  5. Robert S

    Robert S Guest

    > Yep. RobG is, as usual, right. That is why functions like isset()
    > exist in PHP for example.
    >


    Thanks. I think I get the point now. Its probably better not to use
    a checkbox and use another control like radio buttons if you want to
    have a generic method to give a POST variable. The javascript would
    fall over if the client had javascript disabled.

    That no doubt explains the lack of information on the web about this.
     
    Robert S, Apr 23, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claire
    Replies:
    3
    Views:
    4,846
    Wendy Smoak
    Mar 2, 2005
  2. runescience
    Replies:
    0
    Views:
    4,289
    runescience
    Feb 23, 2006
  3. Dan
    Replies:
    5
    Views:
    3,272
    shawpnendu
    May 26, 2009
  4. RVic
    Replies:
    19
    Views:
    1,417
  5. Glenn
    Replies:
    2
    Views:
    126
    Glenn
    Feb 10, 2005
Loading...

Share This Page