tags:

views:

2091

answers:

2

Anyone know what's wrong with this? I have some Classic ASP code that checks for the value of a checkbox, like so:

<!-- HTML page: page1.asp --->
<input id="useBilling" name="useBilling" value="Y" type="checkbox" />

And in my code page (let's call it page2.asp):

' code

useBilling = Request.Form("useBilling")

' useBilling should be "Y" here

If useBilling = "Y" Then
    ' using billing info
Else
    ' not using billing info
End If

The problem is that sometimes, even when I check the checkbox, it's passing an empty string to Request.Form and the wrong code is being executed. I've placed a few Response.Write calls to trace it (this is VBScript, remember) and sometimes it says the value is "Y" but later when I check the value in the conditional, it's empty.

Been wracking my brain trying to figure out why the hell this isn't working, because everything seems to be right, just Request.Form sometimes picks up the value and sometimes doesn't, even when it's checked. Hell, sometimes I'll test it by commenting out the execution code and it will say the value is "Y" then when I uncomment the executing code, it's mysteriously empty again.

EDIT: Weirdly enough, if I include a Response.End tag in the conditional, it will operate as I expect, but when I remove the Response.End it no longer finds the checkbox's value (returns empty) even though a minute ago (with Response.End uncommented) it output a test message that says "Okay, the checkbox was checked". With Response.End commented out, it says "The checkbox wasn't checked".

I even try outputting the value of the checkbox (which should be "Y" if it's checked, and nothing if it's not). And, sure enough if the conditional includes Response.End it will output "Y" and if I remove Response.End, it's empty.

+2  A: 

Been wracking my brain trying to figure out why the hell this isn't working, because everything seems to be right, just Request.Form sometimes picks up the value and sometimes doesn't, even when it's checked.

Not a direct answer, but just to be totally clear: Request.Form("useBilling") will always return an empty value if the checkbox isn't checked. From your "even when I check the checkbox" wording I wasn't quite sure if you were expecting a value there when it wasn't checked. From your code, I think you get it.

As for the issue, I've never seen that happen before despite using ASP for 10+ years (please kill me.) That doesn't mean you're hallucinating, just that I haven't seen it. Interesting!

I wonder if perhaps your HTML (perhaps the form tag in particular) may be malformed. Do you have overlapping tags or a missing closing form tag or anything?

I'd also be extremely curious to see the output of Request.Form when things are misbehaving, ie:

If useBilling = "Y" Then
  Response.Write "Cool, it works!"
Else
  Response.Write "Something's weird. " & Request.Form
End If
John Booty
A: 

That should be correct, if you submit a form and a checkbox is unchecked its value should be that of an empty string -- if it is checked it should have a value of "on"

Andrew G. Johnson
This is incorrect, at least for server-side Vbscript in ASP. If HTML checkbox has value="Y" then Request.Form("useBilling") would be equal to "Y" (not "on") when checked and will be blank when it is not checked.
John Booty