Checkbox boolean value Classic ASP
Sebastian Wright
I have a checkbox
<input type="checkbox" name="chkNGI" value="1">When it is checked I pass the value 1, but when it is not checked any value is passed. I have to pass the value 0.
I've tried
<input type="checkbox" name="chkNGI" <%if prod_ngi_sn.checked then value="1" else value="0" end if%>>But didn't work.
tks
6 Answers
Checkboxes only pass values when ticked. You need logic on the server side to accommodate that.
Dim chkNGI
chkNGI = Request("chkNGI") & ""
If chkNGI = "" Then chkNGI = "0"
End If <script>
function calcParam() { var checked = document.getElementById("prod_ngi_sn").checked; if (checked) document.getElementById("hiddenNGI").value = "1"; else document.getElementById("hiddenNGI").value = "0"; }
</script>
<input type="hidden" name="chkNGI">
<input type="checkbox" name="checkNGI" onClick="calcParam()"> 1 You can try this single line solution
Information: RS=Recordset Object
<input type="checkbox" <%If RS("ColumnName")=True Then Response.Write(" checked='checked' ")%> name="tableColumn" value="1" > 1 I know this question is old, but I recently had to refactor some legacy code for a company in Classic ASP, and ran into this problem. The existing code used a hidden form field with the same name as the checkbox and looked for either "false" or "false, true" in the results. It felt kludgy, but the code also performed actions based on dynamically named checkbox fields with prefixes, so inferring "false" from a missing field would introduce different complications.
If you want a checkbox to return either "0" or "1", this technique should do the trick. It uses an unnamed checkbox to manipulate a named hidden field.
<html>
<body>
<% If isempty(Request("example")) Then %>
<form>
<input type="hidden" name="example" value="0">
<input type="checkbox" onclick="example.value=example.value=='1'?'0':'1'">
<input type="submit" value="Go">
</form>
<% Else %>
<p>example=<%=Request("example")%></p>
<% End If %>
</body>
</html> Create a hidden input with the name "chkNGI". Rename your current checkbox to something different. Add handled for onClick on the checkbox and using a small javascript function, depending on the state of the checkbox, write 0 or 1 in the hidden input.
As an example,
<script> function calcParam() { var checked = document.getElementById("prod_ngi_sn").checked; if (checked) document.getElementById("hiddenNGI").value = "1"; else document.getElementById("hiddenNGI").value = "0"; }
</script>
<input type="hidden" name="chkNGI">
<input type="checkbox" name="checkNGI" onClick="calcParam()"> 4 Your solution in post to saving page;
save.asp
<%
' connection string bla bla
' RS = Recordset Object
If Request.Form("tableColumn")=1 Then RS("ColumnName") = 1
Else RS("ColumnName") = 0
End If
' other columns saving process bla bla bla
%>