Introduction
This code will help you to store checkbox or radio button values via Form (to render on screen / to store in a database). This is very useful if you have many checkboxes/radio buttons in a FORM, with the same name and passing back to the FORM. Or if you want to add all values of a variable in the database in one data field.
This code will show you how to use SPLIT()
function of ASP.
SPLIT()
function is useful when you are going to hold many items in just one variable. Then you can simply break that variable by SPLIT()
function, to build an array. See the example code:
For any questions, feel free to ask me.
Using the Code
<%
myVariable = "myValue1, myValue2, myValue3"
myArray = Split(myVariable,",")
For i = LBound(myArray) TO UBound(myArray)
Response.Write myArray(i) & "<BR />"
Next
Loop was running 3 times, each times value for i= changed, read below...
-------------
-------------
-------------
-------------
-------------
-------------
%>
How can we benefit from the above example?
We can set this variable to our Request.QueryString
or Request.Form
:
Using code as QueryString
:
myVariable = Request.QueryString ("itemName")
Or using code as Form
collection:
myVariable = Request.Form ("itemName")
If request is coming with different values but with same variable, this is where this script helps out...
Now, we should create a Form
:
<form action="mypage.asp">
my hobbies:
<input type="radio" name="hobby" value="martial arts" /> Martial Arts <br />
<input type="radio" name="hobby" value="programming" /> Programming <br />
<input type="radio" name="hobby" value="books" /> books <br />
<input type="submit" value="Go!" />
</form>
Got the point? After pressing Submit, the form will send out hobby as given below:
mypage.asp?hobby=martial arts&hobby=programming&hobby=books
So, do we need to create three variables to catch three values of Request? Of course! not. So, we will catch all in just one variable:
Dim hobby
hobby = Request.QueryString ("hobby")
Response.Write hobby
Now, how can we extract text from one variable? Here comes the solution: easily by SPLIT()
function. Make an array to store and break the variable.
hobby = Split(Request.QueryString("hobby"), ",")
SPLIT
function takes two arguments:
- SPLIT (variableName, by which character we need to break the variable)
So, we just pass the querystring and then we place ",". SPLIT()
will separate the variable into an Array by cutting out ",".
Variable changed into Array as:
hobby(0) = "martial arts"
hobby(1) = "programming"
hobby(2) = "books"
Now, there is no problem to print out these values:
For i = LBound(hobby) TO UBound(hobby)
Response.Write hobby(i) & "<BR />"
Next
And further more, you can add values in a database easily, hope so.
Have fun :)