Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Checkbox Values Returned and Checked 1

Status
Not open for further replies.

DebbieC

Programmer
Mar 29, 2001
168
US
I need the items that were already checked to appear checked when they open the form again for updates. However the way I have the code it only returns the first one that it finds. I need it to look for each of the three options and have them checked if they are in the field.

Here is what I have:

<input type="checkbox" name="ItemType" value="Multiple Choice" <% If objCmd("ItemType") = "Multiple Choice" Then response.write "checked" %>>Multiple Choice<br>

<input type="checkbox" name="ItemType" value="True False" <% If objCmd("ItemType") = "True False" Then response.write "checked" %>>True-False<br>

<input type="checkbox" name="ItemType" value="Alternate Choice" <% If objCmd("ItemType") = "Alternate Choice" Then response.write "checked" %>>Alternate Choice


Does anyone have any ideas?


Thanks.
 
Have you tried giving each of the checkboxes a different name? I have only used the same name for radio buttons, not checkboxes.

TJR
 
checkboxes only return a value when checked.
Avoid using spaces in the value as well. A space is a delimiter inside tags.

Multiple form elements with the same name will have their values returned in a comma delimited string when selected, And as you are presenting the user with a choice you should be using radion buttons.


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
I can't use radio buttons because they can select as many as they want. Radio buttons are used when you only want them to select one.

I will take the spaces out of the values.

I don't see how using different names will help me to retrieve which ones are in the database. For instance, if they selected all three when they originally submitted the form, then when they reopen the form it returns the value "Multiple Choice, True False, Alternate Choice" and I need it to check all three of the boxes. If they only selected one of them originally, then I need it to check the one they originally selected. I also need them to be able to change them. Therefore I need them all to have the same name so that when it's saved, it puts them in the same field in the database.

Any other ideas would be appreciated.
 
Should I change the way the information is added to the database in the first place?

This is what I have:

<input type="checkbox" name="ItemType" value="Multiple Choice">Multiple Choice<br>

<input type="checkbox" name="ItemType" value="True False">True-False<br>

<input type="checkbox" name="ItemType" value="Alternate Choice"><font size="2">Alternate Choice

 
I'm just simply not understanding why you are using checkboxes.

In your first post you show the data coming from one column with 3 different values. to me that's a single choice NOT multiple options as ItemType can only have one of three values.

Your last post tells nothing of how you are adding to the DB. How can we answer the question when up to now you have only posted code that actually appears to contradict the questions you ask.

When you have worked out what you want to do then we will be able to assist.


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
I will try to explain my situation better.

I have used the following code to add the items to the database on the original form (I took the spaces out of the values):

<input type="checkbox" name="ItemType" value="MultipleChoice">Multiple Choice<br>

<input type="checkbox" name="ItemType" value="TrueFalse">True-False<br>

<input type="checkbox" name="ItemType" value="AlternateChoice">Alternate Choice


ItemType = Trim(upl.form("ItemType"))

objCmd.Parameters.Append _
objCmd.CreateParameter("ItemType", adVarChar, adParamInput, 50, PrepareString(ItemType))


So, when the user checks the boxes then it is saving it to the ReqType field in the database. They should be able to check all three boxes if they want. If they check all 3 boxes, it is saving all three values to the ReqType field. It shows "MultipleChoice, TrueFalse, AlternateChoice".

The user can open the form to see what was originally entered and it can be edited. When the form is opened and the data retrieved from the database, I want it to list all three checkboxes again and check the ones that are listed in the field in the database. They can then edit it.

This code works only if there is only one listed in the ItemType field:

<input type="checkbox" name="ItemType" value="MultipleChoice" <% If objCmd("ItemType") = "MultipleChoice" Then response.write "checked" %>>Multiple Choice<br>

<input type="checkbox" name="ItemType" value="TrueFalse" <% If objCmd("ItemType") = "TrueFalse" Then response.write "checked" %>>True-False<br>

<input type="checkbox" name="ItemType" value="AlternateChoice" <% If objCmd("ItemType") = "AlternateChoice" Then response.write "checked" %>>Alternate Choice

I don't know how to get it to check more than one box if more than one is listed in the field (they are comma deliminated).

I hope this is clearer.



 
right I see now. The way you are comparing is the error you can't compare a comma delimited string to a single value and expect it to match.

What you will need to do is split the DB Column data into 3 and use those for the comparison

arrayItem = split(objCmd("ItemType"),3)

or you could use instr

if instr(objCmd("ItemType"),"MultipleChoice") > 0 then
'set checked
end if

etc


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
That is exactly what I wanted. Thank you so much. I'm sorry I didn't make myself clear from the start.

I tried the "instr" and it worked! That's exactly what I was trying to figure out!!

Thanks again.
 
DebbieC, sounds like you straightened it out, but I wasn't recommending you use radio buttons, but what I was assuming was that part of your problem was that you were naming each of your checkbox controls the same name. You wanted to get three different values, by using the same name you had to use the split in order to get around the CSV (comma seperated values).

I was simply trying to say that the same named control for three different checkboxes seems odd, and if you had simply changed the names to be more indicative of the options you were testing you probably could have avoided the issues altogether.

I don't know if that makes sense.

Using radio buttons all with a common name makes sense because only one can be selected.

Using checkboxes all with a common name makes little sense because, as you said yourself, you can set them independently, and in any combination, and having them all the same name makes it difficult to interrogate specific checkboxes.

TJR
 
But I am saving the information to a database and I don't want a seperate field for each one. If they have the same name, then they can easily be saved in the same field.
 
With different names it would only be a matter of concatenating the values returned.
However the problem would be best corrected by a better db design.

As it stands you end up with a field that has to be wide enough to take all 3 values and there is a lot of redundant data in the field. What if at some point those options change or get added to or worse still one is removed. There may be a lot of records that could cause errors at some time in the future.
A normalised database setup would be the correct way (as always).
It would need two more tables, one with the names of the options, and one with the user IDs and a reference to the option id. Cuts down on the amount of data stored and makes future changes and the data processing much much simpler.


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
I see what you're saying. I did normalise it by adding several tables where there were lots of records that were going to be entered with the same fields. However I've never done it with checkbox options especially since I usually have several of them on a form. I will definitely keep that in mind in the future though.

Thank you for your help.
 
DebbieC, again Chris is right about concatenating and a better DB design.

You said: "If they have the same name, then they can easily be saved in the same field."

Actually, just the opposite is the case. The fact that they were the same HTML field made you jump through more hoops, IMHO, than if they were seperate fields, regardless how they were being saved in the DB.

TJR
 
You're right. It was a problem with them in the same field I just didn't realize that it was common practice to create a new table for every set of checkboxes, especially when it only has 3 options.

That's why I love this sight because I learn so much!!!

Thanks for everyone's help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top