how to retrieve CheckBox value using GET Method ?

Discussion in 'ASP General' started by magix, Aug 21, 2008.

  1. magix

    magix Guest

    If I have

    <form action="process.asp" method="get" name="form1">

    ....
    ....
    <input TYPE=checkbox name=sports VALUE=Cricket>
    <input TYPE=checkbox name=sports VALUE=Swimming>
    <input TYPE=checkbox name=sports VALUE=Football>

    </form>

    If I check all, when submit, I will get in URL string

    http://....?... &sports=Cricket&sports=Swimming&sports=Football

    So, in process.asp, how can I differentiate each of them with
    request.querystring("sports)?
    How cna I know how many "sports" have been selected ?
    Assume that all must having same checkbox name, and must use GET method,
    instead of POST

    Thanks.

    regards,
    Magix
    magix, Aug 21, 2008
    #1
    1. Advertising

  2. magix

    Evertjan. Guest

    magix wrote on 21 aug 2008 in microsoft.public.inetserver.asp.general:

    > If I have
    >
    > <form action="process.asp" method="get" name="form1">
    >
    > ...
    > ...
    > <input TYPE=checkbox name=sports VALUE=Cricket>
    > <input TYPE=checkbox name=sports VALUE=Swimming>
    > <input TYPE=checkbox name=sports VALUE=Football>
    >
    > </form>
    >
    > If I check all, when submit, I will get in URL string
    >
    > http://....?... &sports=Cricket&sports=Swimming&sports=Football
    >
    > So, in process.asp, how can I differentiate each of them with
    > request.querystring("sports)?


    I think, but please test first:

    request.querystring("sports")(0)
    request.querystring("sports")(1)

    > How cna I know how many "sports" have been selected ?


    request.querystring("sports").length


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Aug 21, 2008
    #2
    1. Advertising

  3. Evertjan. wrote:
    >> How cna I know how many "sports" have been selected ?

    >
    > request.querystring("sports").length


    More like .Count:
    http://msdn.microsoft.com/en-us/library/ms524784.aspx


    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms.
    Dave Anderson, Aug 21, 2008
    #3
  4. magix

    Evertjan. Guest

    Dave Anderson wrote on 21 aug 2008 in
    microsoft.public.inetserver.asp.general:

    > Evertjan. wrote:
    >>> How cna I know how many "sports" have been selected ?

    >>
    >> request.querystring("sports").length

    >
    > More like .Count:
    > http://msdn.microsoft.com/en-us/library/ms524784.aspx


    Yesss!


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Aug 21, 2008
    #4
  5. magix

    Old Pedant Guest

    "magix" wrote:

    > If I have
    > <input TYPE=checkbox name=sports VALUE=Cricket>
    > <input TYPE=checkbox name=sports VALUE=Swimming>
    > <input TYPE=checkbox name=sports VALUE=Football>
    >
    > So, in process.asp, how can I differentiate each of them with
    > request.querystring("sports)?
    > How cna I know how many "sports" have been selected ?


    Other answers right, but more complex than needed.

    Just do this:

    <%
    ....
    sports = Trim( Request("sports") ) ' do not need to say Request.QueryString
    If sports = "" Then
    ... no sports selected ...
    Else
    sports = Split( sports, ", " ) ' that is COMMA-SPACE and *not* just comma
    Response.Write "You selected " & UBound(sports)+1 " sports."
    End If
    ....
    %>

    This works fine so long as none of the VALUE= values for the checkboxes
    contain COMMA-SPACE.

    But you should be sure you really need to do the SPLIT, first. You may be
    able to do many SQL queries without bothering to get the individual choices.
    Old Pedant, Aug 21, 2008
    #5
  6. Old Pedant wrote:
    > "magix" wrote:
    >
    >> If I have
    >> <input TYPE=checkbox name=sports VALUE=Cricket>
    >> <input TYPE=checkbox name=sports VALUE=Swimming>
    >> <input TYPE=checkbox name=sports VALUE=Football>
    >>
    >> So, in process.asp, how can I differentiate each of them with
    >> request.querystring("sports)?
    >> How cna I know how many "sports" have been selected ?

    >
    > Other answers right, but more complex than needed.
    >
    > Just do this:
    >
    > <%
    > ...
    > sports = Trim( Request("sports") ) ' do not need to say
    > Request.QueryString


    Perhaps not, but I would suggest you do so.
    I'm surprised a pedant would advise someone to be less that precise! :)

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Aug 21, 2008
    #6
  7. magix

    Old Pedant Guest

    "Bob Barrows [MVP]" wrote:

    > Old Pedant wrote:
    > > sports = Trim( Request("sports") ) ' do not need to say
    > > Request.QueryString

    >
    > Perhaps not, but I would suggest you do so.
    > I'm surprised a pedant would advise someone to be less that precise! :)


    LOL! But it *IS* precise...if you know what you are doing.

    http://msdn.microsoft.com/en-us/library/ms524948(VS.85).aspx

    Since QueryString is searched first, then
    Request("foo")
    is equivalent to
    Request.QueryString("foo")
    with the proviso that the latter actually exists.

    Now, pedantically, if for some reason the querystring value does not exist
    then the search process will indeed try to find that name in all the other
    collections. But unless your code has created a "sports" key/value pair in
    Request.Form (that is, via POST) or Cookies, that's not an issue.

    Anyway, the reason I tended to use just Request was simple: During
    development and debug, I'd use METHOD=GET in my <FORM>s so I could see in the
    URL what I was passing. Then, once it was working and debugged, I'd change
    to METHOD=POST but wouldn't have to change my VBS code.

    A hack, but I think a reasonable one.

    Anyway, I'll bet in this case there's no reason for him to know how many
    were selected and/or separate them. I'll bet that if he constructed the SQL
    efficiently he'd just need to do something such as
    sql = " ... WHERE sport IN ('" & Replace(Request("sports"),", ","','")
    & "') "
    or similar.
    Old Pedant, Aug 21, 2008
    #7
  8. "Old Pedant" <> wrote in message
    news:D...
    > "magix" wrote:
    >
    > > If I have
    > > <input TYPE=checkbox name=sports VALUE=Cricket>
    > > <input TYPE=checkbox name=sports VALUE=Swimming>
    > > <input TYPE=checkbox name=sports VALUE=Football>
    > >
    > > So, in process.asp, how can I differentiate each of them with
    > > request.querystring("sports)?
    > > How cna I know how many "sports" have been selected ?

    >
    > Other answers right, but more complex than needed.
    >


    I'm sorry perhaps I'm miss reading you but you seem to be saying the other
    answers are more complex than needed and yours below is simpler??

    > Just do this:
    >
    > <%
    > ...
    > sports = Trim( Request("sports") ) ' do not need to say

    Request.QueryString

    Bob was more gentle, I would say that this is not a good practice at all.
    Always use either .QueryString or .Form. I've seen the short cut above
    cause too many headaches especially when a page was using GET then switches
    to POST.


    > If sports = "" Then
    > ... no sports selected ...
    > Else
    > sports = Split( sports, ", " ) ' that is COMMA-SPACE and *not* just

    comma
    > Response.Write "You selected " & UBound(sports)+1 " sports."
    > End If
    > ...
    > %>
    >
    > This works fine so long as none of the VALUE= values for the checkboxes
    > contain COMMA-SPACE.
    >


    Yikes. Is that 'less complex'? Have you tested it? Does it seem to you to
    match the querystring structure seen in the OP?

    > But you should be sure you really need to do the SPLIT, first. You may be
    > able to do many SQL queries without bothering to get the individual

    choices.
    >


    Thats an interesting assertion. Can you achieve that without creating a SQL
    statement using string concatenation which I'm sure you would agree would be
    a bad thing?


    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Aug 22, 2008
    #8
  9. "magix" <> wrote in message
    news:48ad9aed$...
    > If I have
    >
    > <form action="process.asp" method="get" name="form1">
    >
    > ...
    > ...
    > <input TYPE=checkbox name=sports VALUE=Cricket>
    > <input TYPE=checkbox name=sports VALUE=Swimming>
    > <input TYPE=checkbox name=sports VALUE=Football>
    >
    > </form>
    >
    > If I check all, when submit, I will get in URL string
    >
    > http://....?... &sports=Cricket&sports=Swimming&sports=Football
    >
    > So, in process.asp, how can I differentiate each of them with
    > request.querystring("sports)?
    > How cna I know how many "sports" have been selected ?
    > Assume that all must having same checkbox name, and must use GET method,
    > instead of POST
    >


    Why GET instead of POST? Do you deliberately want enable the possiblity
    that a response can be delivered from the cache?

    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Aug 22, 2008
    #9
  10. Old Pedant wrote:
    > Anyway, the reason I tended to use just Request was simple: During
    > development and debug, I'd use METHOD=GET in my <FORM>s so I could
    > see in the URL what I was passing. Then, once it was working and
    > debugged, I'd change to METHOD=POST but wouldn't have to change my
    > VBS code.


    This seems to contradict your earlier argument that Request("foo") is the
    same as Request.QueryString("foo") because QueryString is the first
    collection searched.

    If you change your form methods to POST, are you not introducing a
    useless -- and potentially interfering -- search through the QueryString
    collection for your post-development scripts?


    > A hack, but I think a reasonable one.


    Well, that's one opinion.

    We have solved countless problems by breaking people of this habit.
    Likewise, default properties.



    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms.
    Dave Anderson, Aug 22, 2008
    #10
    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. Edmilson

    Checkbox - How can i get the value ?

    Edmilson, Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    444
    Edmilson
    Jun 27, 2003
  2. Replies:
    3
    Views:
    391
  3. Imran Aziz

    checkbox value attribute in checkbox control ?

    Imran Aziz, Aug 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    222
    Imran Aziz
    Aug 8, 2005
  4. randy
    Replies:
    13
    Views:
    434
    Tad McClellan
    Nov 1, 2003
  5. Matt
    Replies:
    3
    Views:
    145
    Mike B.
    Apr 13, 2007
Loading...

Share This Page