472,143 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Changing form's text field to checkbox

Related to my earleir post ... this is the same project to re-purpose the Classifieds application in BEGINNING ASP 3.0 (Wrox) to a comicbook database ...

This is a 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:

Expand|Select|Wrap|Line Numbers
  1. <%
  2.   Dim blnNew
  3.      ' detect whether the user is adding a new item or editing an existing one
  4.   Select Case Request.QueryString("Action")
  5.     Case "AddNew"
  6.       blnNew = True
  7.     Case "Edit"
  8.       blnNew = False
  9.   End Select
  10.  
  11. %>
  12.  
  13.     <div id="fullwidth">
  14.     <%
  15.       If blnNew Then 
  16.         Response.Write _ 
  17.           "<center><h1>Add an issue to the database</h1></center>" & _
  18.           "<center><p>Please add the following information for the issue you've just acquired</p> "
  19.       Else 
  20.         Response.Write _ 
  21.           "<center><h1>Edit data for issue you already have</h1></center>" & _
  22.           "<center><p>Please edit the information for this item</p> "
  23.         Dim rsItem 
  24.         Set rsItem = Server.CreateObject("ADODB.Recordset")
  25.         strSQL = "SELECT Title.titleName, Item.idItem, Item.issue, Item.[month], Item.[year], " & _
  26.       " Item.cond, Item.gcdb, Item.image, Item.cents, Item.have, Item.want, Item.note " & _ 
  27.       " FROM Title INNER JOIN Item ON Title.idTitle = Item.idTitle " & _
  28.       " ORDER BY titleName,month;"
  29.  
  30.         rsItem.Open strSQL, objConn, adOpenForwardOnly, adLockOptimistic, adCmdText 
  31.       End If
  32.     %>
  33.  
  34.     <form action="addItem.asp" method="post">
  35. <% 
  36.   If blnNew Then %>
  37.     <input type="Hidden" name="idItem" value=""> <%
  38.   Else %>
  39.     <input type="Hidden" name="idItem" value="<%= Request("Item") %>"> <%
  40.   End If
  41. %>
  42. <table border='0'>
  43.   <tr>
  44.     <td width=20% rowspan=8> </td>
  45.     <td align="right">Title:</td>
  46.     <td colspan="3"><input type="Text" name="titleName" 
  47.           value="<% If Not blnNew Then Response.Write rsItem("titleName") End If%>"
  48.           size="40" maxlength="75"></TD>
  49.   </tr>
  50.   <tr>
  51.     <td align="right">Issue:</td>
  52.     <td><input type="Text" name="issue" 
  53.          value="<% If Not blnNew Then Response.Write rsItem("issue") End If%>"
  54.          size="6"></td>
  55.     <td align="right">Condition:</td>
  56.     <td><input type="Text" name="cond" 
  57.          value="<% If Not blnNew Then Response.Write rsItem("cond") End If%>"
  58.          size="6"></td>
  59.   </tr>
  60.   <tr>
  61.     <td align="right">Month:</td>
  62.     <td><input type="Text" name="month" 
  63.          value="<% If Not blnNew Then Response.Write rsItem("month") End If%>"
  64.          size="6"></td>
  65.     <td align="right">Year:</td>
  66.     <td><input type="Text" name="year" 
  67.          value="<% If Not blnNew Then Response.Write rsItem("year") End If %>" 
  68.          size="6"></td>
  69.   </tr>
  70.  
  71.   <tr>
  72.     <td align="right">GCDB Page:</td>
  73.     <td colspan="3"><input type="Text" name="gcdb" 
  74.          value="<% If Not blnNew Then Response.Write rsItem("gcdb") End If%>"
  75.          size="40"></td>
  76.   </tr>
  77.   <tr>
  78.     <td align="right">Image:</td>
  79.     <td colspan="3"><input type="Text" name="image" 
  80.          value="<% If Not blnNew Then Response.Write rsItem("image") End If%>"
  81.          size="40"></td>
  82.   </tr>
  83.   <tr>
  84.     <td align="right">Cents:</td>
  85.     <td><input type="Text" name="cents" 
  86.          value="<% If Not blnNew Then Response.Write rsItem("cents") End If %>" 
  87.          size="6"></td>
  88.  
  89.     <td> </td>
  90.     <td> </td>
  91.  
  92.   </tr>
  93.   <tr>
  94.     <td align="right">Have:</td>
  95.     <td><input type="Text" name="have" 
  96.          value="<% If Not blnNew Then Response.Write rsItem("have") End If %>" 
  97.          size="6"></td>
  98.  
  99.     <td align="right">Want:</td>
  100.     <td><input type="Text" name="want" 
  101.          value="<% If Not blnNew Then Response.Write rsItem("want") End If %>" 
  102.          size="6"></td>
  103.   </tr>
  104.  
  105.   <tr>
  106.     <td align="right">Notes:</td>
  107.     <td colspan="3"><input name="note" type="text" 
  108.     value="<% If Not blnNew Then Response.Write rsItem("note") End If %>" 
  109.     size="40"></td>
  110.   </tr>
  111.   <tr>
  112.     <td> </td>
  113.     <td align=center colspan=4><BR><%
  114.       If Not blnNew Then %>
  115.         <input type="Submit" name="Delete" value="Delete Item"><%
  116.       End If %>
  117.  
  118.       <input type="Submit" 
  119.          value="<% If blnNew Then %>Add New Item<% Else %>Update Item<% End If %>">
  120.  
  121.       <input type="Reset"></TD>
  122.   </TR>
  123. </table>
  124. </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.

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

Expand|Select|Wrap|Line Numbers
  1. 106   <td align="right">Cents:</td>
  2. 107    <td><% If rsItem("cents") then strChecked = " checked" End If %>
  3. 108    <input type="checkbox" name="cents" value="<%=rsItem("cents")%>"<%=strChecked%>>
  4.     </td>
  5.     <td> </td>
  6.     <td> </td>
  7.  
  8.   </tr>
  9.   <tr>
  10.     <td align="right">Have:</td>
  11.     <td><% If rsItem("have") then strChecked = " checked" End If %>
  12.     <input type="checkbox" name="have" value="<%=rsItem("have")%>"<%=strChecked%>>
  13.     </td>
  14.     <td align="right">Want:</td>
  15.     <td><% If rsItem("want") then strChecked = " checked" End If %>
  16.     <input type="checkbox" name="want" value="<%=rsItem("want")%>"<%=strChecked%>>
  17.     </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:

Expand|Select|Wrap|Line Numbers
  1. Microsoft VBScript runtime  error '800a000d'
  2.  
  3. Type mismatch
  4.  
  5. /publishing/comics/collecting/admin/item2.asp, line 107
If anyone knows how to solve this problem, I'd be ever so grateful ...

Best,

peck2000
Jul 2 '07 #1
1 2480
jhardman
3,406 Expert 2GB
I'm not sure, I'm having a little difficulty picturing the problem, but it seems that the problem lies between how different programs interpret boolean variables. for example, If I remember right, SQL SERVER recognizes 0 and 1, but Access recognizes "True" and "False", whereas HTML checkboxes are interpretted as "on" or "". Could these distinctions be causing the problem?

By the way, I noticed that you don't reset the variable strChecked after it is used. Each time you say something like
Expand|Select|Wrap|Line Numbers
  1. if [some condition] then strChecked = " checked" end if
and then use it again. If the first condition is satisfied then each subsequent time you will get " checked" because you never tell it to reset (strChecked = "") after you use it.

Jared
Jul 3 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Wouter | last post: by
6 posts views Thread by drec | last post: by
5 posts views Thread by hotrod57 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.