WORMrus
WORMrus

Reputation: 169

Getting array of selected checkbox values in ASP server side

i have a form with 3 checkboxes in it and i need to perform some actions depending on selected checkbox values. So my current task is to get an array of selected values on server side using JScript. Here's a form

<form method ="post" action="table.asp" name="grades">
<input type="checkbox" value="2" name="cb" id="cb"> 2</input>
<br>
<input type="checkbox" value="3" name="cb" id="cb"> 3</input>
<br>
<input type="checkbox" value="4" name="cb" id="cb"> 4</input>
<br>
<input type ="submit" value=" Go">
</form>

I've managed to get a string which looks like cb=2&cb=3 using var cbstring = Request.form()

It seems like i can't split it with cbstring.split() method. When i add it server returns unspecified error message without any helpful info in it. Debug is set to true in web.config.

So any help with making things right will be greatly appreciated

Upvotes: 1

Views: 1910

Answers (1)

Kul-Tigin
Kul-Tigin

Reputation: 16950

Request.Form is an object (ASP's request dictionary) so it hasn't a split method. Fortunately Request.Form[(key)].Item is a string of serialized key or key/value pairs of the collection.
Have a look at https://stackoverflow.com/a/31444907/893670 to see how to handle Request collections in ASP w/ JScript, it might be helpful.
And here's a way to do what you want.

var arr = [];
if(Request.Form("cb").Item)
    arr = Request.Form("cb").Item.split(", ");

Upvotes: 1

Related Questions