Click to See Complete Forum and Search --> : set asp session with javascript
flakfiser
07-16-2003, 11:38 AM
how can i do this exactly
here is part of my code that i have already
<% if session("colorbw") = "color" then %>
<input name="color" type="radio" value="color" onClick="MM_setSession"
checked>Color<br>
<input name="color" type="radio" value="bw" onClick="MM_setSession
>Black & White
<% else %>
<input name="color" type="radio" value="color" onClick="MM_setSession
>Color<br>
<input name="color" type="radio" value="bw" onClick="MM_setSession"
checked>Black & White
<% end if %>
basically, i have a session variable called colorbw, and on this page, i
need the user to check a radio button for either color or black and white
reports. now, submitting the form is not an option, but maybe a postback
will work, or something else. i guess i need to know the javascript
function to do this please. colorbw by default = color.
flakfiser
07-16-2003, 07:10 PM
i do understand this. but i know there is a way to do this, probably via a postback, but i don't know how to write the script and html to do this. or, if there is another way to have radio buttons, and once one is clicked, a session variable is set. i just thought javascript would handle this best, and then postback to set the session, because i can't do a form submit on this particular page.
flakfiser
07-17-2003, 11:19 AM
well, how ever it can be done.
when someone clicks on a radio button, i need to do something to set a session variable with the value they clicked on. i can't submit the form though, unless it submits back to itself i guess maybe?
flakfiser
07-18-2003, 11:09 AM
ok great, that reloads the page. now i know how to do that.
now, on to part 2, how to i have 2 choices, lets say BlackWhite and Color, and have it set the session variable accordingly, and retain the chosen checked box when page is reloaded.
AndyR
07-18-2003, 01:35 PM
If your posting to the same page, you can try this:
<input type="radio" name="mycolor" <% if request.form("mycolor") ="White" then response.write "Checked "%> value="White">
I am pretty sure it is "Checked" with radio buttons, but it might be "Selected".
HTH
AndyR
flakfiser
07-18-2003, 02:03 PM
ok, here is the exact code as i have it now, it sort of works, but only keeps BW checked, doesn't keep color checked if i click on it. also, how do i set the session then after it's clicked?
<input type="radio" name="mycolor" <% if request.form("mycolor") ="color" then response.write "Checked "%> value="Color" onClick="
if (this.checked) {
this.form.action=self.location.href;
this.form.submit();
}
return true;">
Color
<br>
<input type="radio" name="mycolor" <% if request.form("mycolor") ="bw" then response.write "Checked "%> value="bw" onClick="
if (this.checked) {
this.form.action=self.location.href;
this.form.submit();
}
return true;">
Black & White
AndyR
07-18-2003, 02:11 PM
To set the session variable, you can put a line like this on the top of your page:
<%
if request.form > "" then 'checks if the form is posted
session("mycolor")=request.form("mycolor")
end if
%>
As for why the black and white is working, the value in thecolor input tag is "Color" and in the IF statement its being compared it to "color". "Color" <> "color". That might bt it. Its usually something annoying like that.
HTH
AndyR
flakfiser
07-18-2003, 02:27 PM
ok, i'm not following what u mean why color won't stay checked, but it does in fact change the session variable now between Color and BW - which is most important.
maybe u can explain to me a different way why i can't keep color checked. if i select color, form submits, and neither color or BW is checked. but mycolor session is set to color.
AndyR
07-18-2003, 02:34 PM
Sorry for the confusion. I'll try another way...
Here is the code for the color input:
<input type="radio" name="mycolor" <% if request.form("mycolor") ="color" then response.write "Checked "%> value="Color"
Notice that the value being assigned value="Color" starts with a upper case "C". Then in the ASP If statement it compares it to "color" with a lower case "c". Try changing one of them and see if that does the trick.
AndyR
flakfiser
07-18-2003, 02:41 PM
yea, i should of caught that. i know enough basic javascript to know it's case sensitive.
thanks.
i'll report soon with final code after a couple of tests
thanks again :)
flakfiser
07-18-2003, 02:57 PM
ok, here is the final code. now, i had to put if then else in there because of the fact that they can back track to previous pages, so i need to see if they did this, to decide which button should be checked.
here is the session set in the head tag
---------------------------------------------------------------
<%
if request.form > "" then 'checks if the form is posted
session("colorBW")=request.form("mycolor")
end if
%>
---------------------------------------------------------------
now here is the actual code in the body
---------------------------------------------------------------
<% if session("colorBW") = "color" then %>
<input type="radio" name="mycolor" <% if request.form("mycolor") <> "bw" then response.write "Checked "%> value="color" onClick="
if (this.checked) {
this.form.action=self.location.href;
this.form.submit();
}
return true;">
Color
<br>
<input type="radio" name="mycolor" <% if request.form("mycolor") ="bw" then response.write "Checked "%> value="bw" onClick="
if (this.checked) {
this.form.action=self.location.href;
this.form.submit();
}
return true;">
Black & White
<% else %>
<input type="radio" name="mycolor" <% if request.form("mycolor") = "color" then response.write "Checked "%> value="color" onClick="
if (this.checked) {
this.form.action=self.location.href;
this.form.submit();
}
return true;">
Color
<br>
<input type="radio" name="mycolor" <% if request.form("mycolor") <> "color" then response.write "Checked "%> value="bw" onClick="
if (this.checked) {
this.form.action=self.location.href;
this.form.submit();
}
return true;">
Black & White
<% end if %>
--------------------------------------------------------------------
if there are any better ways of doing this, please enlighten me, as i would like to know. i do know that this is working though, so thank you very much.
flakfiser
07-19-2003, 10:14 AM
ok, great i will make that change
thanks again guys, you've been a huge help.