cw2504
cw2504

Reputation: 43

classic asp if checkbox is checked else checkbox not checked

Hi I'm new to classic asp and i just can't get my head around this simple problem. I have a form with 2 checkbox's in and this is the result i want:

If the user checks both checkbox's then insert a certain value into the database. Else insert a different value into the database. I've tried a few things but at the moment i'm trying to check if the checkbox doesn't have a value then insert but the else always fires. By default the user has to accept the terms checkbox.

Hope this makes sense. Thanks

<FORM method="POST" action="<% =Request.ServerVariables("SCRIPT_NAME")%>" >

  <p>  <input type="checkbox" name="Privacy" value="1" id="Privacy" >privacy checkbox<a href="javascript:void(0)" onclick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"><img src="/images/qmark.png" /></a></p>

  <p> <input onchange="this.setCustomValidity(validity.valueMissing ? 'Please indicate that you accept the Terms and Conditions' : '');" id="field_terms" type="checkbox" name="Terms" value="2" >I accept Terms and Conditions</p> 

 </form>

Then my asp looks like:

if Request.form("Privacy") <> "" Then

SQL = "INSERT INTO table1 (t1_accept) VALUES('N')"
cnSub.execute SQL
Else

SQL = "INSERT INTO table1 (t1_accept) VALUES('Y')"
cnSub.execute SQL

End If

Upvotes: 1

Views: 2034

Answers (2)

Studio X
Studio X

Reputation: 11

Lets look at three cases:

  1. CASE 1 You have 5 inputs of type TEXT with the same name, e.g., Studiox
    • The request.form(“Studiox”) will give you a string with the values of ALL 5 inputs, separated by a comma (v1, v2, v3, v4, v5).
  2. CASE 2 You have 5 inputs of type CHECKBOX with the same name, e.g., Studiox
    • The request.form(“Studiox”) will also give you a string BUT ONLY with the values of CHECKED inputs, separated by a comma (e.g. v1, v3, v5).
  3. CASE 3. You have 2 (or more) inputs of type CHECKBOX with DIFFERENT names, e.g., Studio_x and Studio_y.
    • The logical conclusion of CASE 1 and CASE 2 is that NOT CHECKED Checkboxes does Not send any value!
    • So, You can test if Studio_x is checked as follows: if request.form(“Studio_x”) <> “” then Checked.

Upvotes: 1

ThatGuyInIT
ThatGuyInIT

Reputation: 2239

Try this:

If Request.Form("Privacy") = "1" Then
    SQL = "INSERT INTO table1 (t1_accept) VALUES('Y')"
Else
    SQL = "INSERT INTO table1 (t1_accept) VALUES('N')"
End If
cnSub.execute SQL

Note this code does an explicit check if Privacy has a value of 1, if it is insert a value of Y into the table, otherwise any other value for Privacy will insert a value of N into the table.

Upvotes: 0

Related Questions