Checkboxes are useful when a visitor may select zero, one, or several options. When several checkboxes share the same name, Classic ASP can receive the selected values as one comma-separated request value.
Example form:
<form method="post" action="form-checkboxck.asp">
<label><input type="checkbox" name="games" value="cricket"> Cricket</label>
<label><input type="checkbox" name="games" value="football"> Football</label>
<label><input type="checkbox" name="games" value="tennis"> Tennis</label>
<label><input type="checkbox" name="games" value="hockey"> Hockey</label>
<button type="submit">Submit</button>
</form>
Read the posted value explicitly from Request.Form:
Dim games, gameList, i
games = Request.Form("games")
If nothing is selected, the value may be empty. Check that before splitting it.
If Len(games) > 0 Then
gameList = Split(games, ",")
End If
Then loop through the resulting VBScript array:
For i = LBound(gameList) To UBound(gameList)
Response.Write Server.HTMLEncode(gameList(i)) & "<br>"
Next
For a fixed set of allowed values, validate each submitted item against that allow-list before using it in application logic or storing it.
A similar PHP example is available in the PHP checkbox-array tutorial.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Tom | 22-10-2012 |
| Using your "Getting the checked values of checkbox Array of a form in ASP" method, could you tell me how I would also get the unique ID of the checked checkbox in "form-checkboxck.asp" | |
| Shashidhara | 22-02-2013 |
| Thank you.. Useful post.. | |