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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find out which/if check box is selected 1

Status
Not open for further replies.

michellerobbins56

Programmer
Jan 10, 2006
89
GB
Hi

I have created an ASP page which outputs data from a database displaying contents of a basket. This all works fine. One of the fields that is displayed is the 'quote basket name' which I have displayed in an input text field to allow a user to edit it. Next to each record that is displayed I have put a tickbox that the user must select meaning they want to edit just that record, they then edit the basket name text input field right next to the checkbox. I can return the 'value' of the check box selected which returns the basket id - this works. I have created an array to hold all the text input field values. I now need some way of getting the index or order of the checkbox selected and then linking it to the appropriate index in the array so as to know this is the correct basket name to update. I am having a problem finding out how to get the 'order' or index of the checkbox selected. I can return the value of the checkbox as already mentioned. Please can someone help me with this problem. Thank you very much for any help.



Here is the code that should be run when a user clicks an update 'button' (the input text field and checkbox field is at the bottom of the code):


'-------------------------------
'get the basket name that user has entered and put into array

formbasketname = Request.Form("sel_quotename")
formarray = Split(formbasketname,", ")
for j=0 to Ubound(formarray)
Response.Write "<br>" & formarray(j) 'TEST display contents of array - (this all works)
next 'move to next element in array


Set rsBasket = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM tvQuoteBasket_GetBasket WHERE user_id = " & userid
rsBasket.Open strSQL, cnnConn, 1, 4
rsBasket.MoveFirst

do

'get the checkbox value selected by the user - this holds the basketid value - (this works)
chkboxvalue = Request.Form("chkbox")(i)


'******NOW NEED TO GET THE BASKET NAME ENTERED BY USER THAT IS ASSOCIATED WITH THE CHECKBOX SELECTED
'******THEREFORE NEED SOME WAY TO GET THE 'ORDER' OF THE CHECKBOXES SELECTED. CURRENTLY 'i' JUST SHOWS THE TOTAL NUMBER OF CHECKBOXES SELECTED


'UPDATE DATABASE CODE GOES HERE (THIS IS NO PROBLEM) BUT NEED CORRECT VALUE OF BASKET NAME TO UPDATE WITH!


rsBasket.MoveNext
i = i + 1


loop until i = (Request.Form("chkbox").Count)
rsBasket.Close







'Code showing the checkbox filed and basket name input field - (this all works)
<INPUT type='checkbox' value='<%= basketid %>' name='chkbox'>
<input type="text" name="sel_quotename" value="<%=quotebasketname%>" maxlength="50" size="50">

'-------------------------------



Thank you for any help.
 
>[tt]<INPUT type="checkbox" name="chkbox" value="<%= basketid %>" onclick="this.checked"> '****is this onclick code correct?[/tt]

No, it is not correct. This might be closer.

[tt]<INPUT type="checkbox" name="chkbox" value="<%= basketid %>" onclick="this.form.hiddenchkbox.value+='-'+this.checked">[/tt]

This will give you k-true (whatever server-side variable k is) if only clicked once and made it checked. If more clicks by changing mind, then it might look like the "k-true-false-true-false", In any case the last one if the final choice. This is no generic thing. It behaves like this just because I only want to script as little as possible. You can elaborate the client side functionality to make it better.

ps apos is not for comment on html page, if you want to make comment, it is this.
[tt]<!-- is this onclick code correct? -->[/tt]
 
Thank you for your reply. My question is how do I get the values (true or false of whether the checkbox is selected or not) from within the ASP code?
 
Parse the value of the hidden box. If you discover true at the end of it, the corresponding checkbox is checked. The way to get the hidden box value is nothing special. The only difference one can make is whether the info is stored there neater and easier to parse out or not.
 
I think I may be I making this all overly complicated (its a very tough problem that I'm sure shouldn't be). My objective is simply to get the value of the basket id and associated basket name so I can update the basket name in the datbase based on the correct basket id.

The basket id is currently held in the checkbox value and the basket name value is held in the text input field value. So I need to link the two so I can get the basket name that corresponds to the correct basket id.

So to summarise all I want to do is update the basket name in the database where the basket id = the value of the check box ticked.

I don't care if tickboxes are not used and if there is another solution to this problem.

Is there a way for the text input field to perhaps hold the basket id somewhere as well as the basket name? So the basket id and basket name can be linked up straight away?

Thank you again for all the replies and help.
 
The simplest.
[tt]<INPUT type="checkbox" id="xyz" name="chkbox" value="<%= basketid %>;xyz">[/tt]
 
Thanks for your help. However I think I have found a solution. I have decided to change the value of the hidden field to hold the basket id. I have put this into an array which I loop through at the same time as looping through the basket name array. Now I can display all the basket id's and all the basket names. The index is the same so each time I loop through the two arrays I get the basket id and correct corresponding basket name that is entered by the user. I think (haven't done it yet!) but I think I can update the database based on this. This negates the need for the checkbox altogether. I'll see how I get on. Thank you for all your help guys.
 
My objective is simply to get the value of the basket id and associated basket name so I can update the basket name in the datbase based on the correct basket id.

The basket id is currently held in the checkbox value and the basket name value is held in the text input field value. So I need to link the two so I can get the basket name that corresponds to the correct basket id.

So to summarise all I want to do is update the basket name in the database where the basket id = the value of the check box ticked.

You obviously didn't read my example that was in 2nd reply.

Checkboxes only pass their value to the next page ifthey have been checked. by naming all of the checkboxes the same name and giving them a value fo the record id, you only receive a list on the next page of the checkboxes that have been checked.
By naming the text fields with a fixed name and adding the id to that name we always know what the textbox's name will be for a specific record. Therefore if an id (say 4) shows up in the checkbox list on the submission page then we know:
1) The checkbox with the value of 4 has been updated
2) The field called txtName_4 needs to be updated in the database for the record with id 4


There is no javascript required for this.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top