Check Boxes and database

Results 1 to 2 of 2

Thread: Check Boxes and database

  1. #1
    Join Date
    Dec 1969

    Default Check Boxes and database

    How can we save the value of multiple check box in database?

  2. #2 Guest

    Default RE: Check Boxes and database

    Say you have on html page:<BR><BR>&#060;form action="doit.asp" method=post&#062;<BR>What car makes interest you: <BR>&#060;input type=checkbox name=cars value="nissan"&#062;nissan<BR>&#060;input type=checkbox name=cars value="mazda"&#062;mazda<BR>&#060;input type=checkbox name=cars value="toyota"&#062;toyota<BR>&#060;/form&#062;<BR><BR>Then in you asp page, it depends on your data base design. You may have a design where you want to link the user to an unlimeted number of choices. You would have a table definition:<BR><BR>UserInterest( UserID int, Make Text(30))<BR><BR>Then you asp would be like this:<BR>&#060;%<BR>For each Item in Request.Form("Make")<BR>&nbsp;&nbsp;&nbsp;Qry = "Insert into UserInterest(UserId,Make) values (" & UserId & ",&#039;" & Request.Form("Make")(Item) & "&#039;)"<BR>&nbsp;&nbsp;&nbsp;objConn.Execute Qry<BR>Next<BR>%&#062; <BR>This would add one row to the database for each ticked checkbox. Note that if you were updating the users records, you would need to delete all the rows relating to that userid, before doing the inserts (otherwise you would end up with multiple entries).<BR><BR>Or you may have a fixed set of choices, against which you want to indicate whether the user tcked them. You would have table definition:<BR><BR>UserInterest ( UserId int, LikesToyota Boolean, LikesMazda Boolean, LikesNissan Boolean)<BR><BR>Your asp would look like this:<BR><BR>&#060;% <BR>Set rs=Server.CreateObject("ADODB.RecordSet")<BR>rs.Op en "Select * from Userinterest where Userid=" & Userid &";", objConn, adOpenDynamic, adLockOptimistic<BR>if (rs.eof and rs.bof) then<BR>&nbsp;&nbsp;&nbsp;rs.addnew<BR>&nbsp;&nbsp ;&nbsp;rs("UserID")=UserID<BR>End if<BR><BR> Rs("Likesnissan") = False<BR> Rs("Likesmazda") = False<BR> Rs("Likestoyota") = False<BR><BR>For each Item in Request.form("Make")<BR>&nbsp;&nbsp;&nbsp;select case Request.form("Make")(Item)<BR>&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;Case "nissan"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp; Rs("Likesnissan") = True<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case "mazda"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp; Rs("Likesmazda") = True<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case "toyota"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp; Rs("Likestoyota") = True<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case Default<BR>&nbsp;&nbsp;&nbsp;End Select<BR>Next<BR>Rs.Update<BR>%&#062;<BR><BR>This will add a new entry for that user if one does not already exist, and then it will set true for each checkbox that was ticked. It set the fields to false first, because only the value for checked boxes get passed from the form.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts