Checkbox value

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

    Checkbox value

    I have an HTML page, that calls an ASP javascript page on "Submit".
    Everything works correctly except that I can't get the value of the
    checkboxes. I've been trying code like this:

    var s = "Off"
    if (Request.Form(" EmailCbx").Chec ked == true)
    {s = Request.Form("E mailCbx").name}

    No matter what I do (I've tried several other things too) the value always
    returns as "Off" What am I doing wrong?

    Also, is there some way to get values from all my checkboxes that are
    checked?


  • RobG

    #2
    Re: Checkbox value

    Ron wrote:[color=blue]
    > I have an HTML page, that calls an ASP javascript page on "Submit".
    > Everything works correctly except that I can't get the value of the
    > checkboxes. I've been trying code like this:
    >
    > var s = "Off"
    > if (Request.Form(" EmailCbx").Chec ked == true)[/color]

    What does - Request.Form("E mailCbx") - return?

    [color=blue]
    > {s = Request.Form("E mailCbx").name}
    >
    > No matter what I do (I've tried several other things too) the value always
    > returns as "Off" What am I doing wrong?[/color]

    If - Request.Form("E mailCbx") - returns a reference to a checkbox then
    it will have a 'checked' property (note the capitalisation) .

    You don't have to explicitly test against 'true'. The checked
    property's value is a boolean:

    if ( Request.Form("E mailCbx").check ed ) {
    // it's checked
    }

    [color=blue]
    >
    > Also, is there some way to get values from all my checkboxes that are
    > checked?[/color]

    Yes. Loop through all of them and test their checked property. If it's
    true, get the checkbox's value. Supposing 'formA' is the name of your
    form, then:

    var el, els = document.forms['formA'].elements;
    for (var i=0, len=els.length; i<len; ++i){
    el = els[i];
    if ('checkbox' == el.type && el.checked){
    // el is a reference to a checked checkbox
    alert('Checkbox ' + el.name + ' is checked');
    }
    }




    --
    Rob

    Comment

    • Ron

      #3
      Re: Checkbox value

      Thanks Rob, this will be very helpful, but the code still isn't working.
      Here's what I tried:

      var s = "Off"
      if (Request.Form(" EmailCbx").Chec ked)
      {s = Request.Form("E mailCbx").name}

      Maybe this is a good clue. When I try this:

      alert(Request.F orm("SoftwareCb x").name);

      I get an error: "Object expected" on that line. Does that mean anything
      important?


      "RobG" <rgqld@iinet.ne t.au> wrote in message
      news:BsuLf.522$ Nv4.80316@news. optus.net.au...[color=blue]
      > Ron wrote:[color=green]
      >> I have an HTML page, that calls an ASP javascript page on "Submit".
      >> Everything works correctly except that I can't get the value of the
      >> checkboxes. I've been trying code like this:
      >>
      >> var s = "Off"
      >> if (Request.Form(" EmailCbx").Chec ked == true)[/color]
      >
      > What does - Request.Form("E mailCbx") - return?
      >
      >[color=green]
      >> {s = Request.Form("E mailCbx").name}
      >>
      >> No matter what I do (I've tried several other things too) the value
      >> always returns as "Off" What am I doing wrong?[/color]
      >
      > If - Request.Form("E mailCbx") - returns a reference to a checkbox then
      > it will have a 'checked' property (note the capitalisation) .
      >
      > You don't have to explicitly test against 'true'. The checked property's
      > value is a boolean:
      >
      > if ( Request.Form("E mailCbx").check ed ) {
      > // it's checked
      > }
      >
      >[color=green]
      >>
      >> Also, is there some way to get values from all my checkboxes that are
      >> checked?[/color]
      >
      > Yes. Loop through all of them and test their checked property. If it's
      > true, get the checkbox's value. Supposing 'formA' is the name of your
      > form, then:
      >
      > var el, els = document.forms['formA'].elements;
      > for (var i=0, len=els.length; i<len; ++i){
      > el = els[i];
      > if ('checkbox' == el.type && el.checked){
      > // el is a reference to a checked checkbox
      > alert('Checkbox ' + el.name + ' is checked');
      > }
      > }
      >
      >
      >
      >
      > --
      > Rob[/color]


      Comment

      • RobG

        #4
        Re: Checkbox value

        Ron wrote:[color=blue]
        > Thanks Rob, this will be very helpful, but the code still isn't working.[/color]

        Please don't top post. Trim quotes and put replies immediately below
        the quote they refer to.

        [color=blue]
        > Here's what I tried:
        >
        > var s = "Off"
        > if (Request.Form(" EmailCbx").Chec ked)
        > {s = Request.Form("E mailCbx").name}[/color]

        As I said previously, find out what - Request.Form("E mailCbx") -
        returns. Insert the following immediately before the 'if' statement and
        report what happens:


        alert( typeof Request ); // 'function' or 'object' ?
        alert( typeof Request.Form ); // 'function' or 'object' ?
        var x = Request.Form('E mailCbx');
        alert( typeof x ); // 'object' ?
        alert( x.name ); // 'EmailCbx' ?
        return;


        If any of the above returns - undefined - or gives something
        unexpected, then that is your problem (or at least part of it).

        [color=blue]
        > Maybe this is a good clue. When I try this:
        >
        > alert(Request.F orm("SoftwareCb x").name);
        >
        > I get an error: "Object expected" on that line. Does that mean anything
        > important?[/color]

        Yes. It means an object was expected but one wasn't returned. However,
        without a bit more information or debugging I can't tell what it was
        expected an object but didn't get one. There may also be more errors
        after that point.

        [...]

        --
        Rob

        Comment

        • Matt Kruse

          #5
          Re: Checkbox value

          Ron wrote:[color=blue]
          > Thanks Rob, this will be very helpful, but the code still isn't
          > working. Here's what I tried:
          > var s = "Off"
          > if (Request.Form(" EmailCbx").Chec ked)
          > {s = Request.Form("E mailCbx").name}[/color]

          I think Rob missed the point that this is ASP javascript. Meaning,
          server-side. Right?

          If that's true, then the .checked and .name properties won't exist.
          If you have this:

          <input type="checkbox" name="EmailCbx" value="Yes">

          then if it's checked, Request.Form("E mailCbx") will contain "Yes".
          If it's not checked, then it won't be submitted at all, and
          Request.Form("E mailCbx") will be null or empty.
          (I'm not sure about ASP/javascript specifics, since I've never used it)

          Hope that helps.

          --
          Matt Kruse




          Comment

          • Ron

            #6
            Re: Checkbox value

            Insert the following immediately before the 'if' statement and[color=blue]
            > report what happens:
            >
            >
            > alert( typeof Request ); // 'function' or 'object' ?
            > alert( typeof Request.Form ); // 'function' or 'object' ?
            > var x = Request.Form('E mailCbx');
            > alert( typeof x ); // 'object' ?
            > alert( x.name ); // 'EmailCbx' ?
            > return;[/color]


            I added the code after the if statement. When I try to tun it, it stops on
            the "return" with a message:

            Microsoft JScript compilation error '800a03fa'
            'return' statement outside of function

            /sendmail.asp, line 23

            return;


            Comment

            • RobG

              #7
              Re: Checkbox value

              Matt Kruse wrote:[color=blue]
              > Ron wrote:
              >[color=green]
              >>Thanks Rob, this will be very helpful, but the code still isn't
              >>working. Here's what I tried:
              >>var s = "Off"
              >>if (Request.Form(" EmailCbx").Chec ked)
              >>{s = Request.Form("E mailCbx").name}[/color]
              >
              >
              > I think Rob missed the point that this is ASP javascript. Meaning,
              > server-side. Right?[/color]

              Aggggh. I assumed a Request object had been implemented client-side and
              that is what was being called.

              [...]


              --
              Rob

              Comment

              • Ron

                #8
                Re: Checkbox value

                Matt,

                Thank you very much! That was the problem. Thanks to Rob too.

                "Matt Kruse" <newsgroups@mat tkruse.com> wrote in message
                news:dtm34m0f31 @news2.newsguy. com...[color=blue]
                > Ron wrote:[color=green]
                >> Thanks Rob, this will be very helpful, but the code still isn't
                >> working. Here's what I tried:
                >> var s = "Off"
                >> if (Request.Form(" EmailCbx").Chec ked)
                >> {s = Request.Form("E mailCbx").name}[/color]
                >
                > I think Rob missed the point that this is ASP javascript. Meaning,
                > server-side. Right?
                >
                > If that's true, then the .checked and .name properties won't exist.
                > If you have this:
                >
                > <input type="checkbox" name="EmailCbx" value="Yes">
                >
                > then if it's checked, Request.Form("E mailCbx") will contain "Yes".
                > If it's not checked, then it won't be submitted at all, and
                > Request.Form("E mailCbx") will be null or empty.
                > (I'm not sure about ASP/javascript specifics, since I've never used it)
                >
                > Hope that helps.
                >
                > --
                > Matt Kruse
                > http://www.JavascriptToolbox.com
                > http://www.AjaxToolbox.com
                >
                >[/color]


                Comment

                Working...