Michael Rosello
Michael Rosello

Reputation: 153

save the checked items in checkbox to session

Need help. I want to save the items that are check in my checkbox to a Session.

What I'm doing is get the value of the items that are checked and store it in an array. Then I will assign that value as Session Name and assign a value of 1 to the session.

This is the code that I'm using but it only gets the first item that is checked.

dim  checkboxList
checkboxList = request.Form("schedule")
checkboxList = split(checkboxList, ",")

for each i in checkboxList
    Session(i) = 1
next

for example If I check A and B on my Checkbox I should get

Session("A")=1 and Session("B")=1

but the only thing i'm getting is Session("A")=1

I tried checking if I'm getting the right item on the my Array by Using this code and the data is correct.

dim  checkboxList
checkboxList = request.Form("schedule")
checkboxList = split(checkboxList, ",")

for each i in checkboxList
    response.write(i)
next

Here is my Html Code.

<form class="well" method="post" action="applicationSave.asp">
 <div class="controls">
                                    <input type="checkbox" id="onShifts" name="schedule" value="onShifts" <% if Session("onShifts") = 1 then response.Write("checked") end if %> /> On Shifts?<br>
                                    <input type="checkbox" id="nightShifts" name="schedule" value="nightShifts" <% if Session("nightShifts") = 1 then response.Write("checked") end if %> /> Night Shifts?<br>
                                    <input type="checkbox" id="partTime" name="schedule" value="partTime" <% if Session("partTime") = 1 then response.Write("checked") end if %> /> Part Time?<br>
                                    <input type="checkbox" id="fullTime" name="schedule" value="fullTime" <% if Session("fullTime") = 1 then response.Write("checked") end if %> /> Full Time<br>
                                    <input type="checkbox" id="holidays" name="schedule" value="holidays" <% if Session("holidays") = 1 then response.Write("checked") end if %> /> Holidays/Sundays?<br>
                                    <input type="checkbox" id="projectBasis" name="schedule" value="projectBasis" <% if Session("projectBasis") = 1 then response.Write("checked") end if %> /> Project Basis
</div>
</form>

Upvotes: 0

Views: 785

Answers (1)

VMV
VMV

Reputation: 576

This is because values delimited with ", " (comma-space), instead "," only. So, before working with array make "trimming" items:

Dim i
For i = 0 To UBound(checkboxList)
checkboxList(i) = Trim(checlboxList(i))
Next

Another way - write Session(Trim(i)) = 1 in for statement.

BTW: Commonly, your code is unsafe. E.g, you have some session bool variable Session("IsAuthorized"). Visitor can send request to your .asp file with value schedule=IsAuthorized...

Upvotes: 2

Related Questions