Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing Check box values from Querystring - HOWTO? 2

Status
Not open for further replies.

Deadline

Programmer
Feb 28, 2001
367
US
Check box issue.

I have a set of check boxes in my form. I am using GET method to submit this form. How do I capture the values of the checkboxes using QueryString Parameter ?

I guess the answer must be fairly simple.. But it freaks me out.. Not happening to my mind today.. Please help... Thank you...
RR

 
RR,

As you may have noticed, when you submit the form you'll see a key/value pair in the URL for each checkbox that was checked. Should look something like this.


It is passing the name of the checkbox with the parameter of on.

<input type=&quot;checkbox&quot; name=&quot;cb1&quot;>Checkbox 1</input>
<input type=&quot;checkbox&quot; name=&quot;cb2&quot;>Checkbox 2</input>
<input type=&quot;checkbox&quot; name=&quot;cb3&quot;>Checkbox 3</input>
<input type=&quot;checkbox&quot; name=&quot;cb4&quot;>Checkbox 4</input>


Unfortunately, it doesn't give you the checkboxes that weren't checked which can be a bummer but you can still examine that. At any rate, you can examine them in the ASP code of the submitted document like this.


<%
IF Request.QueryString(&quot;cb1&quot;) = &quot;on&quot; THEN
' do something, checkbox 1 was checked
END IF
IF Request.QueryString(&quot;cb2&quot;) = &quot;on&quot; THEN
' do something, checkbox 2 was checked
END IF
IF Request.QueryString(&quot;cb3&quot;) = &quot;on&quot; THEN
' do something, checkbox 3 was checked
END IF
IF Request.QueryString(&quot;cb4&quot;) = &quot;on&quot; THEN
' do something, checkbox 4 was checked
END IF
%>


Now, as I said before, for the checkboxes that were not checked, they don't appear in the QueryString collection, but you can still examine it and if it's not there (hence it was not checked) the IF .. THEN statement will evaluate to false as ASP is forgiving if you try to access a form element value that doesn't exist. It just returns a null result.

Hope this provides some progress for you.

TW
 
Todd,
That was a extremely good start. Thank you very very much. But,in my case the checkboxes have the same name because, they are displayed dynamically depending on a condition in the data in the table.

And hence, if I submit them after checking, the querystring contains chk=1&chk=2&chk=3 etc...

If you can suggest me an alternate, it would be great too.. Thank you...
RR

 
Oh My Oh My DesperateCoder, you have opened a little can of worms there. However, you are in luck. I created a very nice solution for examining checkboxes that are created dynamically from data in a database. Hence, the count and description of the checkboxes can be different at any given time.

I will share that success with you, but will need some more detailed information about what you are trying to accomplish so we don't spend days going back and forth.

Can you be specific on what the checkboxes are being used for, and what you intend to do after examining them on the form you are submitting them to ?

TW
 
Sure Todd..
Thank you yet again..

My Data will be something like this :
Code:
NAME      COUNTRY 
~~~~      ~~~~~~~
Todd      US
RR        US
Chiraque  FR
Blair     UK

In the above case, I need to produce a set of 3 check boxes having values like US UK CA; I will be running a distinct query over the COUNTRY column and assign each checkbox a value from the recordset. This is the scenario. Please show me light.

Thank you...
RR

 
I mean sorry the check boxes should have US UK and FR as values...Sorry.. Thank you...
RR

 
Copy this, open it in a browser window, read it and let me know if I'm on the right track here. This is by no means the solution that I was referring to above, I'm just trying to get a better understanding of the layout and what you are doing before I can give you the solution..


<html>

<head></head>

<body>
<form>
<font size=&quot;4&quot;><strong>Like This ??</strong></font><br><br>
Todd: 
<input type=&quot;radio&quot; name=&quot;todd&quot;>US</input> 
<input type=&quot;radio&quot; name=&quot;todd&quot;>UK</input> 
<input type=&quot;radio&quot; name=&quot;todd&quot;>FR</input><br>
RR: 
<input type=&quot;radio&quot; name=&quot;rr&quot;>US</input> 
<input type=&quot;radio&quot; name=&quot;rr&quot;>UK</input> 
<input type=&quot;radio&quot; name=&quot;rr&quot;>FR</input><br>
Blair: 
<input type=&quot;radio&quot; name=&quot;blair&quot;>US</input> 
<input type=&quot;radio&quot; name=&quot;blair&quot;>UK</input> 
<input type=&quot;radio&quot; name=&quot;blair&quot;>FR</input><br><br><br>

<font size=&quot;4&quot;><strong>Or This ??</strong></font><br><br>
Todd: 
<input type=&quot;checkbox&quot; name=&quot;todd&quot;>US</input> 
<input type=&quot;checkbox&quot; name=&quot;todd&quot;>UK</input> 
<input type=&quot;checkbox&quot; name=&quot;todd&quot;>FR</input><br>
RR: 
<input type=&quot;checkbox&quot; name=&quot;rr&quot;>US</input> 
<input type=&quot;checkbox&quot; name=&quot;rr&quot;>UK</input> 
<input type=&quot;checkbox&quot; name=&quot;rr&quot;>FR</input><br>
Blair: 
<input type=&quot;checkbox&quot; name=&quot;blair&quot;>US</input> 
<input type=&quot;checkbox&quot; name=&quot;blair&quot;>UK</input> 
<input type=&quot;checkbox&quot; name=&quot;blair&quot;>FR</input><br><br><br>
</form>
The first example only allows one (per name) to be checked while the second example allows multiple. Which one applies to you or am I way off base here.

</body>
</html>
 
It should be like this...

Code:
<html>
<head></head>
<body>
<form>
<font size=&quot;4&quot;>
<strong>It needs to be like this</strong></font><br><br>
Select all that apply...
<input id=&quot;ctry&quot; type=&quot;CHECKBOX&quot; name=&quot;ctry&quot;>US</input> 
<input id=&quot;ctry&quot; type=&quot;CHECKBOX&quot; name=&quot;ctry&quot;>UK</input> 
<input id=&quot;ctry&quot; type=&quot;CHECKBOX&quot; name=&quot;ctry&quot;>FR</input><br>
<br>
</body>
</html>
Thank you...
RR

 
there's a good article (though it involves databases) about it at:

but in a nutshell: when you get the values back from the form they're seperated by a comma-space (comma followed by a space). so you could use split to put them all in an array. like this:
Code:
Dim arrCountries
arrCountries = Split(Request.Form(&quot;ctry&quot;), &quot;, &quot;)

:)
 
Stakadush, Good suggestion and that works great for multiple select lists. However, it's not that friendly for multiple checkboxes. Unfortunately, the value of a checkbox is &quot;on&quot;. Therefore, you're array would look like this.

on, on, on, on

And since DesperateCoder is displaying these checkboxes dynamically, based on database results, he can't hard code a specific format to extract the values of the array.

With that in mind, the easy way out of this would be to suggest a multiple select list. However, they're ugly and require a Ctrl click or Click/Drag to make multiple selections. Also, the solution I wish to provide to you is scalable, no matter how many or how little country checkboxes you display on a page.

Therefore, I think we should press on.

I have a few more questions for DpCoder based on my understanding so far.

#1: Essentially, is this a page that assigns one or more countries to an individual.

#2: Is the page intended for multiple individuals, or is it more like a personal profile where one or more countries are assigned to a single individual at a time.

#3: Since you can assign more than one country to an individual, how does the data set look for someone who has been assigned more than one country.

Like this:

NAME COUNTRY
~~~~ ~~~~~~~
Todd US
Todd UK

RR US
Chiraque FR
Blair UK

Or this:

NAME COUNTRY
~~~~ ~~~~~~~
Todd US,UK
RR US
Chiraque FR
Blair UK

Or something else ??


If you can answer those, I should be able to format the solution to meet your needs fairly quickly.

TW
 
he could simply add a value property to the checkboxes and that way know which is which.
like this:
Select all that apply...
<input id=&quot;ctry&quot; type=&quot;CHECKBOX&quot; name=&quot;ctry&quot; value=&quot;US&quot;>US</input>
<input id=&quot;ctry&quot; type=&quot;CHECKBOX&quot; name=&quot;ctry&quot; value=&quot;UK&quot;>UK</input>
<input id=&quot;ctry&quot; type=&quot;CHECKBOX&quot; name=&quot;ctry&quot; value=&quot;FR&quot;>FR</input>
you can dynamically generate a code like this from a database or any other source for that matter.
now when you loop through the values you'll get:
US, UK, FR.....

:)
 
Alright.. the requirement is to show the checkboxes ONLY for the countries that are present in the country column. For example we need not show country code CA currently, because no record corresponds to CA.

So again the resulting page after the query is made, will have just three checkboxes for all those 4 sample records because of the DISTINCT clause that'll be used in the Query.

The user after checking US and UK and hitting submit; he should see the records corresponding to US and UK.
Code:
Todd      US
RR        US

Got it ?? Thank you...
RR

 
Well, throw me on an ant hill and smear my face with Jelly !! Stakadush is right !! Thanks.. Therefore, do something like this.

<form>
<%
WHILE NOT RS.EOF
%>
<input type=&quot;checkbox&quot; value=&quot;<%=Trim(RS(&quot;country&quot;))%>&quot; name=&quot;ctry&quot;><%=Trim(RS(&quot;country&quot;))%></input><br>
<%
RS.MoveNext
WEND
%>
</form>


That will list the checkboxes from your recordset and set the value property accordingly.

On the page processing the submittal, you can do something like this.

<%
dim SQL
dim m
SQL = &quot;SELECT fields FROM table WHERE &quot;

FOR EACH m IN Request.QueryString(&quot;ctry&quot;)
SQL = &quot;country = '&quot; & m & &quot;' OR &quot;
NEXT

strip the last OR from the SQL statement
SQL = Left(SQL,Len(SQL)-4)

SQL = SQL & &quot; ORDER BY
somefield&quot;
%>

Now, if the user does not check any of the checkboxes, ASP will throw an error on the above code because the statement would look like this.

SELECT
fields FROM table WH

Therefore, you have two choices there. You can validate client side with JavaScript to prevent the submittal unless at least one checkbox is selected, or you can validate server side in the code above and either return a message to the user or modify the SQL statement to do something different.

To do this with JavaScript, you'll need to do this back on the page with the checkboxes.

This goes between the <head></head> tags

<script Language=&quot;JavaScript&quot;>
<!--

function validate(form_obj) {
var test = 0
for (var m = 0; m < form_obj.elements.length; m++) {
if (form_obj.elements[m].name == &quot;ctry&quot;) {
if (form_obj.elements[m].checked == true) {
test = 1
}
}
}
if (test == 0) {
window.alert(&quot;Please check at least one country&quot;)
return false
}
return true
}

// -->
</script>

The JavaScript above will examine every element in the form, for each element with the name ctry it will see if it's checked. If so, it sets the test value to 1 The last block evalutates the test value, alerts the user if no boxes were checked, and will fail to submit the form.



Of course, this goes in the body of the HTML

<form method=&quot;get&quot; action=&quot;somepage.asp&quot; onSubmit=&quot;return validate(this);&quot;>

list your checkboxes here with the name &quot;ctry&quot;

<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
 
Whoops !! I messed up the SQL build. Replace these lines.


FOR EACH m IN Request.QueryString(&quot;ctry&quot;)
SQL = SQL & &quot;country = '&quot; & m & &quot;' OR &quot;
NEXT


Sorry...

TW
 
Awesome Todd... Many Thanks Stakadush. Superb!!!

I am going to try that... and will let you know how it went.. Thanks for your time and inputs.

Thank you...
RR

 
All the thanks goes to Stak !! That's basic HTML 101 and I was going to give you a solution to build that array in a session variable dynamically while you were building the checkboxes, then use the array on the submittal page to examine the results. He saved both of us a lot of coding.

I feel so stupid !!

Ahhh... The power of forums !!

TW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top