Solved

Fast answer please....How to use VBScript to check FormValues

Posted on 1998-08-28
12
162 Views
Last Modified: 2010-04-09
I use an ASP-page to generate a page consisting of:
One frame consisting of unknown numbers of CheckBoxes
(because they are generated dynamically), some other text-
fields and a submit-button.

When the Submit-button are choosed by the user I want,
on the Click-event on that button, to generate a comma-
separated string of the names of the checked CheckBoxes and
put the string in a hidden text-field in the same frame.

I want all this done before the click on the submit-button
gives control to the ASP-page in the form's ACTION.

Short: I'm looking for a way to go trough all the checked
CheckBoxes without knowing in beforehand how many checkBoxes
there are, and generate a Comma-separated list of the Check-
boxes name,before the control are given to the ASP-file with the submit-button.
 Is this possible to do?????

I know about the request.form syntax that can be used in
ASP-page referenced in the form's ACTION, but this is not
the best solution for me.
 
                If there is a solution to this.... this
          question will be upgraded to 150 pnts if it can
        solve my problem.

Good luck. I use FrontPage98, ASP, Access and VBScript.
If it's not possible in VBScript...what about other scripts
such as Java-script??


               
0
Comment
Question by:erot
  • 5
  • 4
  • 3
12 Comments
 
LVL 28

Accepted Solution

by:
sybe earned 150 total points
Comment Utility
In the first place this is possible using javascript.

Secondly: why would you want to do this: if you use the same name for all the checkboxes, then it is done more or less automatically: you'll get a querystring (if the form method is "GET") like "?CheckBoxName=value1,value2,value3". Where only the values of the checked boxes will be represented.

It can be easily parsed in ASP using:

<%
For each value in Request.Querystring("CheckBoxName")
   Response.write value & "<br>"
Next
%>

It is much harder to parse the value of the hidden field you want to use. Furthermore the javascript solution is browser dependent, and probably won't work in MSIE3.

But ok:

<form name="MyForm" onSubmit="WriteHiddenField()">
<input type"hidden" name="checkboxstring">

.
.
your fields
..

<input type="submit"
</form>

<script>
function WriteHiddenField () {
  var strHiddenField = "";
  i = 0;
  while (i < document.MyForm.length) {
     if(document.MyForm.elements[i].type='checkbox') {
         if(document.MyForm.elements[i]) {
         strHiddenField = strHiddenField + document.MyForm.elements[i].value + ',';
         }
     }
  i++;
  }
  document.MyForm.HiddenField.value=strHiddenField;
}
</script>




0
 
LVL 1

Expert Comment

by:Bluemoon
Comment Utility
hmm i had not read your title that good, it is javascript not vbscript
0
 
LVL 1

Expert Comment

by:Bluemoon
Comment Utility
I solved your problem, and it is possible, you make as many checkboxes as you need in one form and make a second form, you'd submit that makes the comma seperated list.
A for-loop makes sure you dont have to know how many there are just give them the same
name and an array is made.

here is the code:

HEE SOMEONE ELSE ANSWERD THE QUESTION BEFORE ME !!!!!
<HTML><SCRIPT>
function join()
{
 var val,i;
 
 val = "";
 for(i=0;i<document.form1.my_chk.length;i++)
   if (document.form1.my_chk[i].checked)
     val += document.form1.my_chk[i].value + ",";
 
 // save it in the hidden field of the other form
 document.form2.my_chk_list.value = val;
}

</SCRIPT><BODY>

<FORM NAME=form1>
<INPUT TYPE=CHECKBOX NAME=my_chk VALUE=This CHECKED>This<BR>
<INPUT TYPE=CHECKBOX NAME=my_chk VALUE=is CHECKED>is<BR>
<INPUT TYPE=CHECKBOX NAME=my_chk VALUE=a>a<BR>
<INPUT TYPE=CHECKBOX NAME=my_chk VALUE=test CHECKED>test<BR>
<!-- and even more checkboxes named my_chk -->

</FORM>

<FORM NAME=form2 method=POST onsubmit="alert('submitting: list = '+                                                                          document.form2.my_chk_list.value)">
<INPUT TYPE=SUBMIT Onclick="join()" VALUE="Submit">
<INPUT TYPE=HIDDEN NAME=my_chk_list>
</FORM>
</BODY></HTML>

let those 150 pnts come if you like my solution

Bluemoon
0
 
LVL 1

Expert Comment

by:Bluemoon
Comment Utility
O well let Erot fight out who gets the points

Sybe and I haven't seen each others answers and answered the question independetly

mayby you can share the points, Erot, post a question for Bluemoon in the same group for part of the points or ask the EE.

Bluemoon
0
 

Author Comment

by:erot
Comment Utility
I'm still waiting for the book to arrive...I don't have the
syntax of VBScript.
(By the way...do you know any good ASP books for professionals.
I have ordered : Professional Active Server Pages 2.0 (Wrox))


Back to my problem....
Can you please send me the syntax for the
   FORM GET Method....
   If there is a difference between the syntax in a called
   ASP-file and feks if the code on a buttons Click-event please
   show me both syntaxes.
0
 

Author Comment

by:erot
Comment Utility
This was To Sybe.

Can you please send me the syntax for the
   FORM GET Method....
   If there is a difference between the syntax in a called
   ASP-file and feks if the code on a buttons Click-event please
   show me both syntaxes.
 
 

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:sybe
Comment Utility
erot,
I am not sure if I understand your question now. But I'll try to answer it:

1. Submitting a form can be done by "POST" and by "GET". GET is default. GET produces a querystring, POST sends the data to the server from which they can be gotten by "asking" the server in your script. To indicate what you want to do with the from, it should be included in the form-tag (with method=...):
<FORM action="your_page.asp" METHOD="GET">

2. When a form is submitted to an ASP, then that ASP can use the formdata which have been submitted. When the method was "GET", then the formdata can be fetched by Request.Querystring("name_of_the_form_field").
If the method was POST then it is:
Request.Form("name_of_the_form_field").

If you are not sure about the method, you can use Request("name_of_the_formfield")

I hope this answers your questions, but again, I am not sure what you ask.
0
 

Author Comment

by:erot
Comment Utility
Thank You Sybe and Bluemoon...
You to have been very energic  ....
I can see that the question can be missunderstood?!?!?!

Even shorter: Before the form is submitted I want to put a
  commas-eparated list of the names of """only""" the checked
  Check-boxes and put the result in a hidden field in the form.
  As posted before I do not know the names of the check-boxes
  on the form or how many, when I write the script...so the
  script have to be general.

Thank you for your answer so far...I'l try to figure out if the
answers you have given me can be used..Then I'l come back to you.

Erot.


0
 
LVL 28

Expert Comment

by:sybe
Comment Utility
Ok, check the answers given, and see if they do.

I still think that there is a better way to achieve what you want then with the way you want to solve it. But then again, I don't know exactly what you want to achieve.

It would be helpful if you'd post the ASP-code of the page that generates the form with the checkboxes, and the code of the page that will process the formdata. I guess that will make clear what you want to achieve.


0
 

Author Comment

by:erot
Comment Utility
this code works perfectly well on the corresponding ASP-page
on the server.

dim checkValues
checkValues = request.form("C1")
'where C1 are the names of all the checkBoxes on the page.
if the client checked any of the CheckBoxes the variable
checkValues are equal to a commaseparate string of the values
to the checked CheckBoxes.  But is it really not possible to
produce the same commaseparated string with code running on
the client (script within the page that are shown on the client).
If it was it would simplify my code a great deal.

The reason... I want to verify what the user checked, give him
a message where he can submit or cancel. I want to show this message in a messageBox not a new Page...

Erot
0
 
LVL 1

Expert Comment

by:Bluemoon
Comment Utility
If you don't know the names of the checkboxes, the method Sybe used would work
and if you like the have 'only' the hidden value, you would have to use a seperate form
you can even remove the text the 'submit' button sends by making it a normal button
and at the end of the javascript something like
form.submit()

Bluemoon
0
 

Author Comment

by:erot
Comment Utility
Sybe and bluemon...Thank you for answering...
Can't do much about the delivery of points.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now