Read options from drop-down list into an array - vbscript

I'm trying to write code (in Macro Express Pro) to read all of the options in a drop-down box and set them to an array in this format:
;option 1;option 2;option 3;option 4;option 5
So far I was able to make it display in a MsgBox (just as a test, I don't want the message box in production) ,but not append them to an array. Here is what I'm working with (from the site):
<select id="ctl00_cphContent_ddlWorkQueue" class="ddlbox">
<option value="4449">option 1</option>
<option value="4370">option 2</option>
<option value="4371">option 3</option>
<option value="4372">option 4</option>
<option value="4373">option 5</option>
</select>
From the VBS external script:
set OptionChooser = MyIE.Document.GetElementbyid("ctl00_cphContent_ddlWorkQueue")
For Each objOption in OptionChooser.Options
Msgbox objOption.InnerText
Next

Create a dynamic array and fill it with the options, then Join the array:
ReDim arr(OptionChooser.Options.Length - 1)
For i = 0 To OptionChooser.Options.Length - 1
arr(i) = OptionChooser.Options(i).Text
Next
str = Join(arr, ";")
MsgBox str

Related

ASP Classic Code Logic using If statements to check inputs from a form

I have this code in an ASP page written 20+ years ago. I am trying to update the code an I am having trouble figuring this out:
If Request("SUTyp").Count > 1 THEN
CountCriteria = 0
For intMulti=1 to Request("SUTyp").Count
If Request("SUTyp")(intMulti) <> "*" Then
CountCriteria = CountCriteria + 1
If CountCriteria = 1 Then
SUTypCode = "((tblSU.SUTypCode) LIKE '" & Request("SUTyp")(intMulti) & "')"
Else
SUTypCode = SUTypCode & " OR ((tblSU.SUTypCode) LIKE '" & Request("SUTyp")(intMulti) & "')"
End If
Else
SUTypCode = ""
intMulti = Request("SUTyp").Count
End If
SUTyp is a variable that is coming from a form on the previous page. There is an option (from that previous page) in the select box on the form to 'Select All' or to 'Select Multiple Options'.
<Select name="SUTyp" Size="7" Multiple >
<OPTION VALUE="*" SELECTED>all study unit types
<%
do while (not rsSUType.eof) and (SaveError <> -2147467259)
if rsSUType.Fields("SUTypCode").Value = "*" then
%>
<OPTION VALUE="<%response.write(rsSUType.Fields("SUTypCode").Value)%>" SELECTED>.
<%response.write(rsSUType.Fields("SUTypCode").Value)%>,
<%response.write(rsSUType.Fields("SUTyp").Value)%>
<%
Else
%>
<OPTION VALUE="<%response.write(rsSUType.Fields("SUTypCode").Value)%>">.
<%response.write(rsSUType.Fields("SUTypCode").Value)%> -
<%response.write(rsSUType.Fields("SUTyp").Value)%>
<%
End If
rsSUType.movenext
loop
%>
</Select>
It is then using some data to create a variable (SUTypCode =) for a WHERE clause to query the database. What I don't know is the logic of what it is saying. Specifically:
For intMulti=1 to Request("SUTyp").Count
If Request("SUTyp")(intMulti) <> "*" Then
CountCriteria = CountCriteria + 1
If CountCriteria = 1 Then
SUTypCode = "((tblSU.SUTypCode) LIKE '" & Request("SUTyp")(intMulti) & "')"
I am guessing that somehow the ASP form sets some kind of variable intMulti and uses that for a comparison.
If someone could shed some light on this and so I can re-write it that would be great. This is being created using PHP, so I am just trying to figure out what this means so I can create the equivalent.
Thanks!

Rspec + Capybara: How can I use expect to validate all items in a field?

Brothers help me please. I need to validate the values ​​of this combo, I tried it with a command I found here but I still haven't had success. The error I'm getting is: Reason:
Ambiguous match, found 7 elements matching visible option nil
----------------------------------------------
I noticed that the first value is white I don't know if this is the error but I tried to get it for a specific value and I couldn't do it either!!
My Code:
class Screen_Grupo_Intervencao < SitePrism::Page
include RSpec::Mocks::ExampleMethods::ExpectHost
include RSpec::Matchers
element :estrutura, :xpath, '//*[#name="_lyXWFMTGINID_ALAY"]'
all(screen_grupo_intervencao.estrutura select)1.should have_text('CADASTRO TÉCNICO D&C - DISTRIBUIÇÃO E COLETA ENGENHARIA GESTÃO DE PERDAS GSC COMERCIAL GSO OPERACIONAL')
<select style="padding-right:0px;width:100%;" class="f fA" onrealchange="return true;" onchange="setTooltip(this,'');realChangeListener(event);" id="686" name="_lyXWFMTGINID_ALAY" tabindex="151" evidence="">
<option title="" selected="" value="" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;"></option>
<option title="" value="61" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">CADASTRO TÉCNICO</option>
<option title="" value="41" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">D&C - DISTRIBUIÇÃO E COLETA</option>
<option title="" value="62" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">ENGENHARIA</option>
<option title="" value="63" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">GESTÃO DE PERDAS</option>
<option title="" value="1" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">GSC COMERCIAL</option>
<option title="" value="21" class="f fA" style="border:0px;border-radius:0px;font-size:1em;box-shadow:none;">GSO OPERACIONAL</option>
</select>
It would be a big help if you read the Stackoverflow instructions on inserting code into questions so it would format it correctly. Also the code you're showing somehow has a linked '1' stuck in the middle of it.
The error you're getting is because you have specified element :estrutura, :xpath, '//*[#name="_lyXWFMTGINID_ALAY"]' which means there should be only 1 of them. However on your page there are apparently 7 elements matching the xpath. Either make the selector more specific so it only matches one thing, or change it to elements :estrutura, :xpath, '//*[#name="_lyXWFMTGINID_ALAY"]' so it allows multiple elements to be found.
I would also suggest to stop using xpath when it's not necessary - elements :estrutura, '[name="_lyXWFMTGINID_ALAY"]'
Assuming you are meant to have multiple matching elements on the page and I'm reading you're code right then I think you'd be doing
screen_grupo_intervencao.estrutura[1].should have_text('CADASTRO TÉCNICO D&C - DISTRIBUIÇÃO E COLETA ENGENHARIA GESTÃO DE PERDAS GSC COMERCIAL GSO OPERACIONAL')
although that seems like a strange way to be testing.

I can't get option value without using xpath in Cabybara

I'll try to explain again. I have to get the value Arizone but I'm only getting it via xpath. The element was mapped with: element :select_cad_state, "#uniform-id_state". I don't want to use xpath to get the value Arkansas, I want to use something like: select_state.send_keys(DATA[:cad_user][:_state]) ???..etc..etc..
I want to get the arizona value from the users.yml file and pass it as an argument in the front.
############ code page #####################
<div class="selector" id="uniform-id_state" style="width: 269px;"><span style="width: 259px; user-select: none;">Florida</span><select name="id_state" id="id_state" class="form-control" style="">
<option value="">-</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option></div>
################### my PageObjects #########################
class ScreenCadastro < SitePrism::Page
set_url 'http://automationpractice.com/index.php?controller=authentication&back=my-account'
element :input_cad_company, "#company"
element :input_cad_address, "#address1"
element :input_cad_city, "#city"
element :select_state, "#uniform-id_state" (my problem is here)
################ yaml file ##############
:cad_user:
:_password: 457226
:_company: SQATest
:_address: International Drive 678
:_city: Bradenton
:_state: Arizona
################## my env file ###################
DADOS = YAML.load(File.open(File.join(File.dirname(__FILE__) + "/massa/users.yml")))
input_cad_company.send_keys(DADOS[:cad_user][:_company])
input_cad_address.send_keys(DADOS[:cad_user][:_address])
input_cad_city.send_keys(DADOS[:cad_user][:_city])
find(:xpath,'/html/body/div/div[2]/div/div[3]/div/div/form/div[2]/p[7]/div/select/option[3]').click
You are asking about option value, but from all your code it looks like you actually want to select based on the string contents of the option element (not the value). As I posted in my answer to your previous question this should just be
select_state.select(DADOS[:cad_user][:_state])
If that is not working for you please provide the error message it's giving you.

How can I add paging for results in a table created in Classic ASP?

I have some code done in VBScript that creates a table. Specifically, the code pulls information from a database and then loops through the result adding them to a table. The problem is that there are 14,000 rows in this table. Every time this page tries to load, I get a 500 Internal Server error which I assume is due to lack of memory.
For the loop, I have this:
<%
fHideNavBar = False
fHideNumber = False
fHideRequery = False
fHideRule = False
stQueryString = ""
fEmptyRecordset = False
fFirstPass = True
fNeedRecordset = False
fNoRecordset = False
tBarAlignment = "Left"
tHeaderName = "DataRangeHdr1"
tPageSize = 0
tPagingMove = ""
tRangeType = "Text"
tRecordsProcessed = 0
tPrevAbsolutePage = 0
intCurPos = 0
intNewPos = 0
fSupportsBookmarks = True
fMoveAbsolute = False
If IsEmpty(Session("DataRangeHdr1_Recordset")) Then
fNeedRecordset = True
Else
If Session("DataRangeHdr1_Recordset") Is Nothing Then
fNeedRecordset = True
Else
Set DataRangeHdr1 = Session("DataRangeHdr1_Recordset")
End If
End If
If fNeedRecordset Then
Set DataConn = Server.CreateObject("ADODB.Connection")
DataConn.Open "DSN=MYDSN","MyUserName","MyPassword"
Set cmdTemp = Server.CreateObject("ADODB.Command")
Set DataRangeHdr1 = Server.CreateObject("ADODB.Recordset")
cmdTemp.CommandText = "SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID"
cmdTemp.CommandType = 1
Set cmdTemp.ActiveConnection = DataConn
DataRangeHdr1.Open cmdTemp, , 0, 1
End If
On Error Resume Next
If DataRangeHdr1.BOF And DataRangeHdr1.EOF Then fEmptyRecordset = True
On Error Goto 0
If Err Then fEmptyRecordset = True
If Not IsEmpty(Session("DataRangeHdr1_Filter")) And Not fEmptyRecordset Then
DataRangeHdr1.Filter = Session("DataRangeHdr1_Filter")
If DataRangeHdr1.BOF And DataRangeHdr1.EOF Then fEmptyRecordset = True
End If
If fEmptyRecordset Then
fHideNavBar = True
fHideRule = True
End If
Do
If fEmptyRecordset Then Exit Do
If Not fFirstPass Then
DataRangeHdr1.MoveNext
Else
fFirstPass = False
End If
If DataRangeHdr1.EOF Then Exit Do
%>
<tr>
<td><p align="center"><%= DataRangeHdr1("FIRM") %></td>
<td><p align="center"><%= DataRangeHdr1("PHONE") %></td>
<td><p align="center"><%= DataRangeHdr1("FAX") %></td>
<%end if%>
</tr>
<%
Loop%>
Now, I believe that the programmer before me essentially copied the code from this website: http://www.nnybe.com/board%20members/DEFAULT.ASP
In fact, I actually changed the column names in my loop to match the website, since it was so similar (my real column names are different). After the loop, the code I have is as follows:
</TABLE>
<%
If tRangeType = "Table" Then Response.Write "</TABLE>"
If tPageSize > 0 Then
If Not fHideRule Then Response.Write "<HR>"
If Not fHideNavBar Then
%>
<TABLE WIDTH=100% >
<TR>
<TD WIDTH=100% >
<P ALIGN=<%= tBarAlignment %> >
<FORM <%= "ACTION=""" & Request.ServerVariables("PATH_INFO") & stQueryString & """" %> METHOD="POST">
<INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE=" << ">
<INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE=" < ">
<INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE=" > ">
<% If fSupportsBookmarks Then %>
<INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE=" >> ">
<% End If %>
<% If Not fHideRequery Then %>
<INPUT TYPE="Submit" NAME="<% =tHeaderName & "_PagingMove" %>" VALUE=" Requery ">
<% End If %>
</FORM>
</P>
</TD>
<TD VALIGN=MIDDLE ALIGN=RIGHT>
<FONT SIZE=2>
<%
If Not fHideNumber Then
If tPageSize > 1 Then
Response.Write "<NOBR>Page: " & Session(tHeaderName & "_AbsolutePage") & "</NOBR>"
Else
Response.Write "<NOBR>Record: " & Session(tHeaderName & "_AbsolutePage") & "</NOBR>"
End If
End If
%>
</FONT>
</TD>
</TR>
</TABLE>
<%
End If
End If
%>
</TABLE>
I'm guessing from the < and > around the PagingMove part, this is supposed to allow paging. However, I'm not even seeing this on my page. I don't know if the code on the link above works on their website, but for my own website I'd ask:
How can I modify this code to provide an option to click through pages of the data result so the server doesn't run out of memory?
If there is a more elegant solution to this that can accomplish the same thing, I'd appreciate that as well!!!
In your SQL you could add a LIMIT offset
SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 0,10 ' Results 1 to 10
SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 10,10 ' 11 - 20
SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 20,10 ' 21 - 30
...
If you're using MySQL you can use...
SELECT SQL_CALC_FOUND_ROWS PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 0,10
... to get a total count of the results and calculate the number of page links to display:
(total_results/results_per_page) ' and round up.
Then link to the pages below the results table and pass the page numbers as a query string:
default.asp?page=1
default.asp?page=2
default.asp?page=3
...
Have some code at the top of your page that gets the requested page number and calculates the correct offset value:
<%
Const results_per_page = 10
Dim limit_offset, page_num
limit_offset = 0 ' default
page_num = request.querystring("page")
if isNumeric(page_num) then
page_num = int(page_num)
if page_num > 0 then
limit_offset = (page_num-1)*results_per_page
else
page_num = 1 ' default
end if
else
page_num = 1 ' default
end if
%>
Finally, apply the limit offset to your SQL:
cmdTemp.CommandText = "SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT " & limit_offset & "," & results_per_page
You could also use GetRows() to convert the recordset to a 2D array and apply a limit when looping
Dim r, rs_loops, theData
theData = DataRangeHdr1.getRows()
rs_loops = page_num*results_per_page
if rs_loops > uBound(theData,2) then rs_loops = uBound(theData,2)
for r = limit_offset to rs_loops
' output data from the DataRangeHdr1 recordset
%>
<tr>
<td><p align="center"><%= theData(2,r) ' firm %></td>
<td><p align="center"><%= theData(0,r) ' phone %></td>
<td><p align="center"><%= theData(1,r) ' fax %></td>
</tr>
<%
next
But this would mean storing large amounts of unseen data in memory. Using a LIMIT offset in the SQL would make more sense.

Adding a text area in table and saving it to SQL Server database

I'm using Classic ASP to add a note function to the table that is displaying rows from a database. The inserted row will save to the database saved Remarks but the following code isn't working.
<%
Dim fRemark
fRemark = Request.Form("Remarks")
Dim rsIntra,MyQryItr2
set cnIntra = Server.CreateObject("ADODB.Connection")
set MyQryItra2 = server.CreateObject ("ADODB.Recordset")
set rsIntra = Server.CreateObject("ADODB.Recordset")
MyQryItra2 = "select Remarks from [PurchaseOrderTrackInfo]"
rsIntra.Open MyQryItra,strRMSIDMcn
if rsIntra.eof then
MyQryItr2 = "insert into [PurchaseOrderTrackInfo] Remarks values N'" & fRemark & " '; "
cast(Remarks as int)
cnIntra.Execute MyQryItr2
else
rsIntra.close
set rsIntra = Nothing
set rsIntra = server.CreateObject("ADODB.Recordset")
MyQryItr2 = "UPDATE [PurchaseOrderTrackInfo] SET Remarks = N'" & fRemark & " '; where Remarks = rowID;"
end if
set rsIntra=Nothing
strConnDB= "Driver={SQL Server};Server=GB;Database=PurchaseOrderTrackInfo;UID=madfox;PWD=;"
%>
<td colspan="10" bordercolor=#3399ff bgcolor=#FFFF99 align="center">
<font face="Arabic Transparent" size="1" color="#800080"></font>
<form action=UpdatePO1.asp method=post >
<textarea name="Remarks" cols="20" rows="2" ><%=fRemark%></textarea>
<input type="submit" class="btn1" value="save" name="finish"/>
<input type="hidden" name="rowID" value="ID" />
</td>
</form>
<%
you never execute your update query. also your update statement does not seem to be valid as you are using the column Remarks as storage for the Remark and as row id. consider adding a rowid column to you table and use the following update statement
MyQryItr2 = "UPDATE [PurchaseOrderTrackInfo] SET Remarks = N'" & fRemark & " ' where rowId =" & rowID
cnIntra.Execute MyQryItr2
Since your code is vulnerabe to SQL injection, you should look up parameterized queries.

Resources