PDA

View Full Version : Checkbox is null


christrinder
03-14-2003, 06:01 PM
Hello

If the user checks a checkbox in my form, I can return the value (i.e. 1) to the database. However, if they leave it unchecked, the db field ends-up being blank. Is there any way I can return a zero if the box isn't checked. I tried an...

if request.form("checkbox") = ""
then...

but if they don't check it, then there is not value to run against the statement and it causes an error. Any ideas?

Chris

raf
03-14-2003, 07:51 PM
Did you try:

If Len(request.form("checkbox"))=0 then

--> checks for zero lenght

Or, maybe a better approach, set a default value for the related variables in the database.

whammy
03-15-2003, 12:58 AM
Yeah... I do that sometimes, have a default value of 0 in the db, and then if the checkbox has a value of "1", I update it.

Another method...


<%
Function IsChecked(val1,val2)
If val1 = val2 Then IsChecked = " checked=""checked"""
End Function

<input type="checkbox" name="cb_1" value="X"<% = IsChecked(cb_1,"X") %> /> One<br />
<input type="checkbox" name="cb_2" value="X"<% = IsChecked(cb_2,"X") %> /> Two<br />
<input type="checkbox" name="cb_3" value="X"<% = IsChecked(cb_3,"X") %> /> Three<br />
<input type="checkbox" name="cb_4" value="X"<% = IsChecked(cb_4,"X") %> /> Four<br />
<input type="checkbox" name="cb_5" value="X"<% = IsChecked(cb_5,"X") %> /> Five<br />

Then when requesting...

' This is for redisplaying the values:

cb_1 = Request.Form("cb_1")
cb_2 = Request.Form("cb_2")
cb_3 = Request.Form("cb_3")
cb_4 = Request.Form("cb_4")
cb_5 = Request.Form("cb_5")

Then to write to ONE field in the database, you can loop through the entire number and use "&" to assign each one to the variable... I'm too busy now to code this but I will give you an example tomorrow!

christrinder
03-15-2003, 04:31 PM
Thanks guys, I'll give them a try on Monday. By the way, I did set the default value in the DB to zero, but the form action page puts the inputted value in there, which, when unchecked, is a blank. I'll try the Len(request.form... that Raf suggested. I have never come across that before. Do I not need a second ")" bracket in there?

raf
03-15-2003, 04:59 PM
Yep, you do. My bad.
The function is Len(string |variable) --> see below.

I'm not sure i understand what your problem is exactly.
If you set the default to 0 in the DB, then it will remain zero until it gets updated.

If you only want to update a variable when the checkbox (related to that variable) you should run a check before you compose the sql statement.
(like checking the lenght of the value from that variable.).
with a "for each bow in request.form" loop you can also check on every checkbox and only include the ones that were checked in you sql statement.

here's some code that does that (updates one variable with the recordID as condition. On the previous page i generated the form from a recordset and gave them the ID as name and 1 as value.


sql = "update table set variable=1 where "

For each box in request.form
if request.form(box) = "1" then
if row = 1 then
sql = sql + "(Id=" + box + ")"
else
sql = sql + " OR (Id=" + box + ")"
end if
row = row + 1
end if
next




more info from the helpfile
---------------Description
Returns the number of characters in a string or the number of bytes required to store a variable.
Syntax
Len(string | varname)
The Len function syntax has these parts:

Part Description
string Any valid string expression. If string contains Null, Null is returned.
varname Any valid variable name. If varname contains Null, Null is returned.


Remarks
The following example uses the Len function to return the number of characters in a string:
Dim MyString
MyString = Len("VBSCRIPT") ' MyString contains 8.


--------------------------------------------------------------------------------

Note The LenB function is used with byte data contained in a string. Instead of returning the number of characters in a string, LenB returns the number of bytes used to represent that string.

--------------------------------------------------------------------------------