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 dencom 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.
 
what are you trying to do here, is it a editable datagrid, if it is you prob be better off using some ajax to update the basket fields (onblur it would update the db)
 
The easiest solution would be to add the basketid to the end of all of your field names for eveything except the checkbox. This way you could get the checkbox value on the next page and use the values in that list to get all of the necessary fields.

Example:
Code:
[b]Page1.asp[/b]
<%
Option Explicit
%>
<html>
<body>
<form method="POST" action="page2.asp">
<%
Dim myConn, myRS
Set myConn = Server.CreateObject("ADODB.Connection")
myConn.Open "my conneciton string"
Set MyRS = MyConn.Execute("SELECT someId, someName, someDesc FROM someTable")

If Not MyRS.EOF Then MyRS.MoveNext
Response.Write "<table>"
Response.Write "<tr><th>Edit</th><th>Name</th><th>Description</th></tr>"
Do Until MyRS.EOF
   Response.Write "<tr>"
   Response.Write "<td><input type=""checkbox"" name=""chkEdit"" value=""" & MyRS("someId") & """/></td>"
   Response.Write "<td><input type=""text"" name=""txtName_" & MyRS("someId") & """ value=""" & MyRS("someName") & """/></td>"
   Response.Write "<td><input type=""text"" name=""txtDesc_" & MyRS("someId") & """ value=""" & MyRS("someDesc") & """/></td>"
   Response.Write "</tr>"

   MyRS.MoveNext
Loop
Response.Write "<tr><td colspan=""3""><input type=""submit"" value=""Submit Changes""></td></tr>"
Response.Write "</table>"
%>
</form>
</body>
</html>

[b]Page2.asp[/b]
<%
Option Explicit

Dim MyConn
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open "my connection string"

'1 -- loop through all of the id's that were selected (they will be passed as a list)
Dim someId, someText, someDesc
For Each someId In Request.Form("chkEdit")
   '2 -- get the values for the text and desc inputs that match this id
   someText = Request.Form("txtName_" & someId)
   someDesc = Request.Form("txtDesc_" & someId)

   '3 -- clean-up the values (escape single quotes)
   someText = Replace(someText,"'","''")
   someDesc = Replace(someDesc,"'","''")

   '4 -- execute an update statement
   myConn.Execute "UPDATE SomeTable SET someText = '" & someText & "', someDesc = '" & someDesc & "' WHERE someId = " & someId
Next

%>

Disclaimer: I wrote that on the fly, so their may be some syntactical errors or mispellings, but hopefully it will get the concept across

-T

barcode_1.gif
 
Thank you for your replies. I have no problem returning the basket id. No problems at all with the basket. My only problem is updating the database with the basket name (this is an 'editable' field in the display grid) based on what the user has entered in the field AND which checkbox has been ticked. Each check box holds the basket id (as its value). This I can return no problem - the question is I can't 'link' up the checkbox in any way to the basket name text input field.

Ideally I would want to get the 'ID' or order of the checkbox that was ticked. And then use this index to return the associated index in the array (the array holds all the basket name input field values).

For example if I have 5 records being displayed. The user ticks the third checkbox down. I need a way of knowing that this is the THIRD checkbox. Then with the 'number 3' I can use this to return the item in the array with an index of 2 (as an array always starts at 0, so this would in effect therefore be the third basket name item in the array - and therefore the basket name 'linked' with the third checkbox that was ticked). My problem is I have no idea how to work out that it was the third checkbox ticked.

Is there any way to do this?

How would an onBlur example work?

Thank you again for any help.
 
couldn't you simple give the checkboxes and incremental value then just use that value for the referencing the array example:

Code:
<input type="text" name="sel_quotename" /><input type="checkbox" name="chkbox" value="0" /><br />
<input type="text" name="sel_quotename" /><input type="checkbox" name="chkbox" value="1" /><br />
<input type="text" name="sel_quotename" /><input type="checkbox" name="chkbox" value="2" />
your asp can be like this
Code:
formbasketname = Request.Form("sel_quotename")
formcheckbox=Request.Form("chkbox")

formarray = Split(formbasketname,", ")  
checkarray = Split(formcheckbox,", ")    
for j=0 to Ubound(checkarray)
	Response.Write formarray(checkarray(j)) & "<br>"
next
 
Thank you for your help. That would work fine however I currently need to keep the value of the check box as <%= basketid %> as this is used in other areas of the code.

I can try assigning an incrementing value to the 'id' of the checkbox. However how do I return what the id is of the checkbox? Request.Form("chkbox") returns the 'value' of the checkbox. What asp code returns the 'id' of the checkbox?

I have done many searches on the internet but cannot see how to do this.

Thank you very much for any help.
 
It's for client-side's consumption, the id.
 
What I would say is that if you really want the id of the checked checkbox, you have to put that data into a form field, say hidden text, and send back to server.
 
nice tsuji!

populate the ids of the checkbox in chron order then run a js function to check whether or not checkbox is checked, if checked set value of a hidden field to the id of the checkbox and then validate server-side against the hidden field values
 
I have tried the hidden field solution already but without success. I will have another go just to see if I did something wrong the first time.

Thanks for your replies.
 
How do I check if a checkbox is selected or not? I have tried various ways all seem not to work.

Thank you again for any help.
 
[tt]<input type="checkbox" id="abc" name="xyz" value="pqr" onclick="alert(this.checked+'\n'+this.id+'\n'+this.name+'\n'+this.value)">what</input>
[/tt]
 
off the top of my head, try something like this

Code:
function checkforchecked(e,hidid){
    if(e.checked){
      document.getElementById(hidid).value=e.id;
    }else{
      document.getElementById(hidid).value="";
    }
}

usage:
Code:
<input type="checkbox" name="chkbx" value="45" onclick="checkforchecked(this,'1')">
<input type="hidden" id="1" name="1" value="">
 
>[tt]id="1" name="1"[/tt]
That's no good. Must start with some alphabet or underscore, if you don't mind. That's not only for w3c standards sake, it also has real negative repercusion on the client/server-side scripting.
 
Thank you for all the replies. I have nearly solved it. I just need to know how to do the ASP part and find out if the checkbox has been selected (i.e is true or false). I've done the Javascript and html Input text field part as above... but how do I reference or find out if the
formbasketname = Request.Form("sel_quotename")

has been selected as I loop through the code?

 
try something like this

Code:
hidvals=split(request.form("hid"),", ")
formbasketname = split(Request.Form("sel_quotename"))

for j=0 to Ubound(hidvals)
	if trim(hidvals(j))<>"" then
		response.Write formbasketname(hidvals(j))
	end if
next

fyi: hidden values need to have the same name - in this case its "hid
 
Thank you for your help with this. However it is not that part of the ASP code I am struggling to work out how to do. I have done all the array work and all that seems to work. It is just the checkbox issue - how can I code the ASP code to see if the checkbox is checked (i.e = true or false)? I can do it from within the Javascript code and it returns a variable true or false depending on whether the checkbox has been selected. When I try to call the javascript value (i.e whether it is true or false) I just get a blank response which is not surprising... is there some way of getting the value of whether the checkbox is selected or not from within the ASP code? Then I can get the appropriate hidden field value when the checkox selected value =true.
 
Here are my two input fields that are in a loop which goes through the database and displays all records in a recordset to display the basket. The loop and display of data all works fine - it is just the checkbox problem and finding out if it is checked... anyway here are my two input fields (the first is the hidden field that tells me WHICH checkbox is selected - by using a value called 'K' that increments each time the loop is carried out, and the second input field is the checkbox which holds the basketid value):
<INPUT type="hidden" name="hiddenchkbox" value="<%= k %>">
<INPUT type="checkbox" name="chkbox" value="<%= basketid %>" onclick="this.checked"> '****is this onclick code correct?

Now here is the input field for the basket name which can be edited by the user:
<INPUT type="text" name="sel_quotename" value="<%=quotebasketname%>">


Now here is my code that gets all the values:

formbasketname = Request.Form("sel_quotename")
chkboxvalue = Request.Form("chkbox")
hiddenchkboxvalue = Request.Form(hiddenchkbox")

This all works now. I can then do the array part putting all the values into an array. But my problem is how to do the IF statement to check if the checkbox is selected or not... I can't seem to find out how this last but very important part works.

Thank you very much for any help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top