How to give a value to non selected checkbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M

    How to give a value to non selected checkbox

    Hello,

    I would like to get a value for a non selected checkbox in a form.

    Imagine my form contains :

    <input type='checkbox' name='t[0]' value='1'>
    <input type='checkbox' name='t[1]' value='1'>

    When I analyse $t, if first checkbox is not selected and second is, I have :
    $t[0] has no value. $t[1] equals 1
    and count($t) equals 1

    I would like to find a way to get :
    $t[0] equals 0. $t[1] equals 1
    and count($t) equals 2

    Any idea ?

    Thanx
    M
  • strawberry

    #2
    Re: How to give a value to non selected checkbox

    Can you explain why you want to get the value? It might help with the
    answer.

    Comment

    • Ken Robinson

      #3
      Re: How to give a value to non selected checkbox


      M wrote:[color=blue]
      > Hello,
      >
      > I would like to get a value for a non selected checkbox in a form.
      >
      > Imagine my form contains :
      >
      > <input type='checkbox' name='t[0]' value='1'>
      > <input type='checkbox' name='t[1]' value='1'>
      >
      > When I analyse $t, if first checkbox is not selected and second is, I have :
      > $t[0] has no value. $t[1] equals 1
      > and count($t) equals 1
      >
      > I would like to find a way to get :
      > $t[0] equals 0. $t[1] equals 1
      > and count($t) equals 2[/color]

      You can "pre-initialize the values by using a hidden field. That way if
      the checkbox isn't checked, the value from the hidden field will get
      passed back. If it is checked, you get the checked value:

      <input type="hidden" name="t[0]" value="0">
      <input type="hidden" name="t[1]" value="0">
      <input type='checkbox' name='t[0]' value='1'>
      <input type='checkbox' name='t[1]' value='1'>

      Ken

      Comment

      • M

        #4
        Re: How to give a value to non selected checkbox

        Thank you Ken.
        This is *exactly* what I needed.

        M
        [color=blue]
        > M wrote:[color=green]
        >> Hello,
        >>
        >> I would like to get a value for a non selected checkbox in a form.
        >>
        >> Imagine my form contains :
        >>
        >> <input type='checkbox' name='t[0]' value='1'>
        >> <input type='checkbox' name='t[1]' value='1'>
        >>
        >> When I analyse $t, if first checkbox is not selected and second is, I have :
        >> $t[0] has no value. $t[1] equals 1
        >> and count($t) equals 1
        >>
        >> I would like to find a way to get :
        >> $t[0] equals 0. $t[1] equals 1
        >> and count($t) equals 2[/color]
        >
        > You can "pre-initialize the values by using a hidden field. That way if
        > the checkbox isn't checked, the value from the hidden field will get
        > passed back. If it is checked, you get the checked value:
        >
        > <input type="hidden" name="t[0]" value="0">
        > <input type="hidden" name="t[1]" value="0">
        > <input type='checkbox' name='t[0]' value='1'>
        > <input type='checkbox' name='t[1]' value='1'>
        >
        > Ken
        >[/color]

        Comment

        • Marcin Dobrucki

          #5
          Re: How to give a value to non selected checkbox

          M wrote:
          [color=blue]
          > I would like to get a value for a non selected checkbox in a form.[/color]

          Have a look at the Javascript code generated by PEAR::QuickForm for
          the advcheckbox element.

          /Marcin

          Comment

          Working...