Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox boolean value Classic ASP

Tags:

asp-classic

I have a checkbox

<input type="checkbox" name="chkNGI" id="prod_ngi_sn" 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" id="prod_ngi_sn" <%if prod_ngi_sn.checked then value="1" else value="0" end if%>>

But didn't work.

tks

like image 384
williancsilva Avatar asked Jan 18 '13 17:01

williancsilva


4 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
like image 198
webaware Avatar answered Nov 09 '22 04:11

webaware


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" id="hiddenNGI"> 
<input type="checkbox" name="checkNGI" id="prod_ngi_sn" onClick="calcParam()">
like image 41
GeorgeVremescu Avatar answered Nov 09 '22 03:11

GeorgeVremescu


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" >
like image 26
Sedat Kumcu Avatar answered Nov 09 '22 03:11

Sedat Kumcu


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>
like image 2
phatfingers Avatar answered Nov 09 '22 04:11

phatfingers