...

View Full Version : Changing form's text field to checkbox



MattiMan
07-03-2007, 09:43 AM
OK, same project to re-purpose the Classifieds application in BEGINNING ASP 3.0 (Wrox) to a comicbook database ...

This is another brainteaser that should have been easy to resolve but just doesn't seem to work for me ... The original application includes a form page for editing the details of an "item". In my case the item is a comicbook. The page functions just fine as long as I stick with text fields, but I run into problems when I try to change a couple of the text fields into checkboxes.

So, here's the code of the Item.asp page with text fields:


<%
Dim blnNew
' detect whether the user is adding a new item or editing an existing one
Select Case Request.QueryString("Action")
Case "AddNew"
blnNew = True
Case "Edit"
blnNew = False
End Select

%>

<div id="fullwidth">
<%
If blnNew Then
Response.Write _
"<center><h1>Add an issue to the database</h1></center>" & _
"<center><p>Please add the following information for the issue you've just acquired</p> "
Else
Response.Write _
"<center><h1>Edit data for issue you already have</h1></center>" & _
"<center><p>Please edit the information for this item</p> "
Dim rsItem
Set rsItem = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT Title.titleName, Item.idItem, Item.issue, Item.[month], Item.[year], " & _
" Item.cond, Item.gcdb, Item.image, Item.cents, Item.have, Item.want, Item.note " & _
" FROM Title INNER JOIN Item ON Title.idTitle = Item.idTitle " & _
" ORDER BY titleName,month;"

rsItem.Open strSQL, objConn, adOpenForwardOnly, adLockOptimistic, adCmdText
End If
%>

<form action="addItem.asp" method="post">
<%
If blnNew Then %>
<input type="Hidden" name="idItem" value=""> <%
Else %>
<input type="Hidden" name="idItem" value="<%= Request("Item") %>"> <%
End If
%>
<table border='0'>
<tr>
<td width=20% rowspan=8> </td>
<td align="right">Title:</td>
<td colspan="3"><input type="Text" name="titleName"
value="<% If Not blnNew Then Response.Write rsItem("titleName") End If%>"
size="40" maxlength="75"></TD>
</tr>
<tr>
<td align="right">Issue:</td>
<td><input type="Text" name="issue"
value="<% If Not blnNew Then Response.Write rsItem("issue") End If%>"
size="6"></td>
<td align="right">Condition:</td>
<td><input type="Text" name="cond"
value="<% If Not blnNew Then Response.Write rsItem("cond") End If%>"
size="6"></td>
</tr>
<tr>
<td align="right">Month:</td>
<td><input type="Text" name="month"
value="<% If Not blnNew Then Response.Write rsItem("month") End If%>"
size="6"></td>
<td align="right">Year:</td>
<td><input type="Text" name="year"
value="<% If Not blnNew Then Response.Write rsItem("year") End If %>"
size="6"></td>
</tr>

<tr>
<td align="right">GCDB Page:</td>
<td colspan="3"><input type="Text" name="gcdb"
value="<% If Not blnNew Then Response.Write rsItem("gcdb") End If%>"
size="40"></td>
</tr>
<tr>
<td align="right">Image:</td>
<td colspan="3"><input type="Text" name="image"
value="<% If Not blnNew Then Response.Write rsItem("image") End If%>"
size="40"></td>
</tr>
<tr>
<td align="right">Cents:</td>
<td><input type="Text" name="cents"
value="<% If Not blnNew Then Response.Write rsItem("cents") End If %>"
size="6"></td>

<td> </td>
<td> </td>

</tr>
<tr>
<td align="right">Have:</td>
<td><input type="Text" name="have"
value="<% If Not blnNew Then Response.Write rsItem("have") End If %>"
size="6"></td>

<td align="right">Want:</td>
<td><input type="Text" name="want"
value="<% If Not blnNew Then Response.Write rsItem("want") End If %>"
size="6"></td>
</tr>

<tr>
<td align="right">Notes:</td>
<td colspan="3"><input name="note" type="text"
value="<% If Not blnNew Then Response.Write rsItem("note") End If %>"
size="40"></td>
</tr>
<tr>
<td> </td>
<td align=center colspan=4>
<%
If Not blnNew Then %>
<input type="Submit" name="Delete" value="Delete Item"><%
End If %>

<input type="Submit"
value="<% If blnNew Then %>Add New Item<% Else %>Update Item<% End If %>">

<input type="Reset"></TD>
</TR>
</table>
</form>

The object here is to create a dual-purpose page that can be used to Add New Items to the database and to Edit Existing Items. This works okay, but the text fields for "cents", "have" and "want" come up with the words "True" or "False" pre-filled when in Editing mode. That's okay, but checkboxes would be better, right?

My problem arises when I try to change the "cents", "have" and "want" fields to checkboxes. This bit of code almost works:


106 <td align="right">Cents:</td>
107 <td><% If rsItem("cents") then strChecked = " checked" End If %>
108 <input type="checkbox" name="cents" value="<%=rsItem("cents")%>"<%=strChecked%>>
</td>
<td> </td>
<td> </td>

</tr>
<tr>
<td align="right">Have:</td>
<td><% If rsItem("have") then strChecked = " checked" End If %>
<input type="checkbox" name="have" value="<%=rsItem("have")%>"<%=strChecked%>>
</td>
<td align="right">Want:</td>
<td><% If rsItem("want") then strChecked = " checked" End If %>
<input type="checkbox" name="want" value="<%=rsItem("want")%>"<%=strChecked%>>
</td>


(The variable "strChecked" is DIMmed further up the page!)

I say "almost" because it works fine (returns a checked checkbox when the related field in the database is checked) when I'm looking at an existing database entry, but it fails when I try to use it to add an entirely new item, with this error message:


Microsoft VBScript runtime error '800a000d'

Type mismatch

/publishing/comics/collecting/admin/item2.asp, line 107

If anyone knows how to solve this problem, I'd be ever so grateful ...

Best,

MattiMan

Spudhead
07-03-2007, 12:35 PM
If rsItem("cents")

If that's not coming out as a boolean, you'll get an error. What datatype is the field in the database?

At the very least, I'd wrap a cBool() round it (and try to save your server just a little more effort by telling it where to look for the field):


If cBool(rsItem.Fields("cents"))

But if it's coming out as NULL or something then that will still throw an error and you'll need to add some validating code that ensures your application only deals with the sort of data that it expects to.

MattiMan
07-03-2007, 08:13 PM
Thanks for the reply, Spudhead ...

The field value is definitely True/False as it's set up in the database as a yes/no tickbox ...

Best,

MattiMan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum