Saving checkbox state on form post problem - html

I'm having problems setting the unchecked state of checkboxes upon form post. I have over 1000 checkboxes and so far i am able to check each checkbox n save there checked state. My problem however is unchecking the checkboxes.
I'm not having problems saving the checked state of each checkbox after form is posted. My problem is unchecking values then having that unchecked state saved after form post.
I'm also using paging on the page as shown in the code below (Values of submit buttons are parameters of a stored procedure)-
<center><input id="buttonStyle" type="submit" name="Page" value="#">
<% for i = 97 to 122 %>
<input id="buttonStyle" type="submit" name="Page" value="<%=CHR(i) %>">
<% next %></center>
<input id="buttonStyle" type="submit" name="Page" value="View All">
I need to save my checked state as I view each letter. So far thanks to the help of shadowwizard I am able to save checked and unchecked states of checkboxes if I click view all button. However the problem now is that when i click another letter (e.g - a, b, c) any checked state is removed when clicking view all.
<%
Dim tempArray
Dim checkArray
Dim temphiddenArray
Dim hiddenArray
'CheckBox Selection Array
if len(Request.Form("selectedRecord")) > 0 then tempArray = split(Request.Form("selectedRecord"), ",")
if isarray(tempArray) then
redim checkArray(ubound(tempArray))
'Create CheckArray ARRAY
for i = lbound(tempArray) to ubound(tempArray)
checkArray(i) = trim(tempArray(i))
next
'Hidden Array check
if len(request.Form("hiddenArray")) > 0 then temphiddenArray = split(request.Form("hiddenArray"), ",")
if isarray(temphiddenArray) then
redim hiddenArray(ubound(temphiddenArray))
for i = lbound(hiddenArray) to ubound(hiddenArray)
hiddenArray(i) = trim(temphiddenArray(i))
Session("LastCheck_"&i) = hiddenArray(i)
next
if ubound(hiddenArray) >= ubound(checkArray) then
for i = lbound(hiddenArray) to ubound(hiddenArray)
Session("CheckBox_"&(Session("LastCheck_"&i))) = ""
next
end if
end if
for i = lbound(checkArray) to ubound(checkArray)
Session("CheckBox_"&checkArray(i)) = "Checked"
%>
<input type="hidden" name="hiddenArray" value="<%=checkArray(i) %>">
<%
next
else
Session("CheckBox_"&request.Form("hiddenArray")) = ""
end if
%>
<% while not objRS.EOF %>
<input type="checkbox" name="selectedRecord" value="<%=objRS("Id") %>" <%=Session("CheckBox_"&objRS("Id")) %>>
objRS.MoveNext
wend

You're going through whole forest instead of climbing a short tree, or in other words making something simple terribly complicated.
What you need is this function:
<%
Function WasCheckedByUser(id)
Dim x
WasCheckedByUser = "checked=""checked"""
For x=1 To Request.Form("selectedRecord").Count
If Trim(Request.Form("selectedRecord").Item(x))=Trim(id) Then
Exit Function
End If
Next
WasCheckedByUser = ""
End Function
%>
Then use it like this:
<input type="checkbox" name="selectedRecord" value="<%=objRS("Id") %>" <%=WasCheckedByUser(objRS("Id")) %>/>
No need in arrays, no need in Session variables. Just simple iteration over the posted values (which are the values of the checked checkboxes) and if certain value is there, it means the corresponding checkbox should be checked, otherwise leave it unchecked.
Edit: you lose the "state" because when you click specific letter, you send to the browser only part of the checkboxes - so the previous value is indeed "lost" for good.
The most simple way to fix that is to send all checkboxes to the browser - always - and using simple style hide those that you currently don't send at all.
So, you'll have to change the SQL used to populate objRS to always select all, and change the loop to this:
<%
Do Until objRS.EOF
Response.Write("<input type=""checkbox"" name=""selectedRecord"" value=""" & objRS("Id") & """ " & WasCheckedByUser(objRS("Id")))
If Len(Request.Form("Page"))=1 And Trim(Left(objRS("Id"), 1))<>Trim(Request.Form("Page")) Then
Response.Write(" style=""display: none;""")
End If
Response.Write(" />")
objRS.MoveNext
Loop
%>
As far as I can see it should work and now preserve the state even for the hidden checkboxes.

check the resulting source code to see what it generated.
From what I can see, one problem would be that only checked checkboxes are posted when you submit a form, so if you originally have 10 checkboxes and check 3 of them and post, only those 3 will post back (and from the code, I guess only those 3 checkboxes would display after you submit, all of them checked).
You'd need to save all the checkboxes in the first run (say, with a default empty value like space), and then just change their session values when they're checked

The checkboxes are they actual items such as a category or option?
The reason i ask is that if you're setting checkboxes, how are these checkboxes defined? are they static fields or are they dynamic. I hope dynamic.
Here's how you can get things going.
Create a table called news (newsid, newstxt)
Create a table called categories (catid, catxt)
Create a table called NewscatSelected (ncsid, newsid, catid)to store check boxes of selected categories with the related newsid
Checkbox page form (cb1.asp)
Send the newsid as a query string to page (cb1.asp?newsid=34)
Dynamically list all categories as checkboxes, and get all records from NewscatSelected and pair them up. Any items that exist in the table NewscatSelected will be checked. They will have the same name but with an underscore and the numeric value of the category (cbt_34) to distinguish them easily
3.
Update Page Form (cb2.asp)
Upon getting querystring delete any existing values for the newsid sent to avoid duplicates
Insert the new checked values into the table called "NewscatSelected"
Redirect back to (cb1.asp?newsid=34) and new values will be checked.
See image below

I just posed an answer to this problem here:
how to implement a check box in classic asp
It's specifically designed for non-sequentially numbered, dynamically created checkboxes which require passing both a unique ID value and either checked or unchecked.
The solution does require a hidden field for each check box, but it does not use Javascript or any client side programming.

As the previous answer states, checkboxes do not get posted back when they are unchecked, so you have nothing in the request object that represents unchecked ones, which is fine if you know everything you are expecting to be posted back, but if it's all dynamic then it's not much use.
One workaround i've used before is to use hidden fields for the actual data, and use the checkboxes to control (via JS) the value's of the associated hidden fields, however I don't know how that solution would perform if you have 1000's of them on a page!

Related

Pass value to textbox in website using Excel VBA

I am trying to navigate a website and perform activities based on input from Excel.
I am stuck in passing values to input element which auto populates value in next text box based on value we have passed (eg: if I pass a zip code in target box then city name is auto populated in next box). I can pass values but it is not triggering the JS which auto populates value.
Target box code in webpage
<input name="zipcode" class="inputBoxRequired" id="zipcode" onkeypress="return isNumberKey(event)" onblur="getZipCity(this.value,'/ProviderPortal')" type="text" size="46" maxlength="5">
Code tried
Set Zip = ie_Doc.getElementById("zipcode")
Zip.Value = Zip_Code 'Here value gets updated but associated js is not triggered
ie_Doc.getElementById("zipcode").Focus
even tried fire event
Zip.FireEvent ("onchange")
In webpage it is mentioned as Onkeypress event. I googled to find solution but I can't get exact one.
Zip.FireEvent ("onkeypress")
Zip.FireEvent ("onblur") '<== Apparently this did the trick
Those are the shown events.
I have come across the same scenario for solution verified this post whereas in my case I do not have "onblur" tag, so I did some search and come up with an solution.
Dim event_onChange As Object
Set event_onChange = ie_Doc.createEvent("HTMLEvents")
event_onChange.initEvent "keypress", True, False
Zip.dispatchEvent event_onChange
It will help others who are facing same problem.

Referencing the value of a hyperlinked text box

So I'm having some difficulties with this code. I know it's obnoxiously wordy, but every attempt I made to turn some of these form references into variables to save space ended with me having even less functionality than before.
Basically what I've done so far is create a navigation form with several tabs, one to create a ticket, one to resolve/edit a ticket, and one to search the tickets. The search tab is basically a continuous form that updates based on the search criteria I enter. My goal is that when I click on the ticketID for each record, it will take me to the selected record on the Resolve/Edit Ticket page (on that page I have a combo box [called cboGoToRecord] where you can select the record you want).
I have a hyperlink in place that takes the user to the Resolve/Edit page and code that works ONLY when the line I've denoted with four asterisks (for clarity) is replaced with
rst.FindFirst "ticketID =" & [some number].
When I do that, the results are as expected. If I leave it as it is below, every record looks up the first record (A Debug.print check shows that the value of this field is apparently always 1...) So I guess what I need to figure out is how do I access the ticketID hyperlink's value so that I can put it on that line and make my code function effectively? I apologize if this is overly detailed but figured too much was better than not enough.
Private Sub ticketID_Click()
'Takes user from Search Tickets to Resolve/Edit Issues tab
DoCmd.BrowseTo acBrowseToForm, "frmResolveIssues", "frmBrowseTickets.NavigationSubform"
On Error Resume Next
Dim rst As Object
Set rst = Forms!frmBrowseTickets!NavigationSubform.Form.RecordsetClone
[Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value = [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
****rst.FindFirst "ticketID =" & [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
Forms!frmBrowseTickets!NavigationSubform.Form.Bookmark = rst.Bookmark
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
End Sub
Edit:
After altering my form to add a separate hyperlink and referencing the static ticketID, I have concluded that everything I thought was true was not. Finding the value of a hyperlink was NOT the problem. The problem is that my ticketID value truly does insist on being one, and I have no clue how to fix that.
When this works:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
then also check out:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
As June7, I never use the Navigation form. It complicates everything too much.

Use VBA to navigate a website and modify drop down box

Good Morning,
I am working a VBA project where I would like to access an internal company, login required, web site and extract data from a table. I have successfully written code that will navigate to the correct website, and then modify a drop down box value from "All Transactions" to "Running Record". The problem I have is that I cannot not get the data in the table to refresh once the drop down box is updated. On the website, next to the drop down field, is a "Go" hyperlink. Whenever I try to activate the link, using vba, it resets the drop down and I don't get the data displayed that I want.
I was able to find a similar post where a user explained that the issue was related to the site being secured and that a .FireEvent would need to be used. Although the concept made sense to me implementation was another story. The biggest difference between my situation and the other's was that when his drop down box was changed it would activate the event. In my case there is a seperate ID, the "Go" hyperlink I mentioned above, that needs to be activated once the drop down value is entered.
Below is a portion of the HTML code where drop down box options are located as well as the "GO" hyperlink.
<select name="classList" onchange="selectClass(this);"><option class=tblrow2 value="All" selected=selected>All Schedules</option>
<option value="A" >
Transactions-Financial
</option>
<option value="N" >
Transactions-Non-Financial
</option>
<option value="RB" >
Running Balance
</option></select>
<input type="hidden" name="classindx" id = "classindx" value="" />
Go
This is a portion of my code that modifies the dropdown box value.
'try to get form by ID
Set frm = ie.Document.getElementByID("myFormID")
'try to get form by name
If frm Is Nothing Then Set frm = ie.Document.getElementsByName("facilityFeeRepayForm").Item(0)
'Set ieButton = ie.Document.getElementById("classindx") Here I tried to treat the Go hyperlink like a button but that didn't work either.
If frm Is Nothing Then
Else
ie.Visible = True
For Each element In frm.elements
On Error Resume Next
Select Case element.Name
Case "classList": element.Value = "RB"
ie.getElementByID("classindx").FireEvent("onClick") 'nothing happens with this code
While ie.ReadyState <> 4: DoEvents: Wend
End Select
Next
End If
I would appreciate any help you can offer.
Thank you,
John
Instead of clicking the link, try this:
ie.Document.parentWindow.execScript "submitTransForm()", "JavaScript"

Cannot reference to a form control which is out of focus

This is a follow up to a previous question I asked about how to update a textbox while typing. In my previous question, I had three textboxes, two of which were enabled and one which was not. They look like this:
My goal is to use the First and Last Name of the two textboxes to fill a "Code Personal" in the other textbox. As I type the first and last names, I want the Code Personal textbox to update immediately, with the format of LLLLLL_F (eg: BERNAS_P). However, whenever I try to update the Code Personal textbox, I receive this error:
The code I use to create the Code Personal format and to update the textbox is:
Private Sub TxtFName_Change()
firstName = Me.TxtFName.Value
lastName = Me.txtLName.Value
firstPart = Left(lastName, 6)
secondPart = Left(firstName, 1)
nameCode = firstPart + "_" + secondPart
upperNameCode = UCase(nameCode)
txtCodePersonal.Text = upperNameCode 'My debug tells me I have an error here
End Sub
I've tried to set the focus to the txtCodePersonal textbox through: [txtCodePersonal].SetFocus, but I still receive an error (MS Access can't move the focus to the control txtCodePersonal.)
Any Ideas as to how I can update the "Code Personal" textbox while typing in the other two textboxes?
Thanks in advance for all your help.
Do your value assignment to the text box's .Value property instead of its .Text property. Either of these should work:
Me.txtCodePersonal.Value = upperNameCode
Me.txtCodePersonal = upperNameCode
.Value is the default property for a text box, so you don't need to include it explicitly. Include it if you feel it makes the code clearer.

ASP.Net MVC passing a database string to a CheckBox value

This is closely related to a question I posted yesterday regarding CheckBoxes.
Here is my setup:
I have a database directory, with a list of names, among other fields, as my model.
I have a search page where users can search this directory and select a name.
I have a form page that displays the name with a checkbox next to it which allows the user to decide if they want to include the name as a value in the submitted form.
A controller that handles the submitted form.
Goal:
What I would like to know is, how can I get the string value of the name that was selected in the directory to both display in the form page View, and also include this string in the value field of the CheckBox?
You cannot change what the value of a checked check box; check boxes always get posted with "on" as their values if checked, and MVC can use this to automatically map the posted values to booleans. However, if this list of names is dynamic, and each name is unique, you can generate the form with dynamically-named check boxes. Then, in the controller, you can inspect the raw form to see which names were checked. For example, in the view, your code could look something like this:
<form>
<% /* Some loop here */ { %>
<input type="checkbox" name="name_<%= Html.Encode(theName) %>" />
<label><%= Html.Encode(theName) %></label>
<% } %>
</form>
Then, in the controller:
foreach (string name in listOfAvailableNames)
{
if (Request.Form["name_" + name] == "on")
{
// Handle the name being selected
}
else
{
// Handle the name not being selected
}
}