Asp-classic Vbscript webpage language switcher into variable - vbscript

For example i have language switcher in index.asp
<ul class="drop-lang_menu">
<li id="LanguageSelected"><img src="img/icons/flags/ru.png" />Rus
<ul>
<li><img src="img/icons/flags/ru.png" />Rus</li>
<li><img src="img/icons/flags/lt.png" />Lit</li>
<li><img src="img/icons/flags/us.png" />Eng</li>
</ul>
</li>
</ul>
and also in this file i have translate function for "Login" button text translate
<%=transl("Login")%>
This function is explained in file function.inc which included in index.asp
<%
Dim Lang
Lang = Document.getElementById("LanguageSelected").innerText
Function transl(TxT as String)
Dim d
d = Application("TranslateList")
If d = "" Then
d = FetchTranslateList(TxT)
Application("TranslateList") = d
End If
transl = d
End Function
Function FetchTranslateList(TxT as String)
Dim rs, fldName, s
Set rs = CreateObject("ADODB.Recordset")
rs.Open "select "+Lang+" from Translations where txt='"+TxT+"'", _
"dsn=name;uid=sa;pwd=;"
s = "<select name=""Translations"">" & vbCrLf
Set fldName = rs.Fields("+Lang+")
Do Until rs.EOF
s = s & " <option>" & fldName _
& "</option>" & vbCrLf
rs.MoveNext
Loop
s = s & "</select>" & vbCrLf
rs.Close
Set rs = Nothing
Set fldName = Nothing
FetchTranslateList = s
End Function
%>
Questions is:
Lang = Document.getElementById("LanguageSelected").innerText seem not working! So how to get selected language value and translate webpage for each user separately ?
Is that is right way to make webpage translation depending on language which user select on the site?

Use asp to change language (document.getElement... is javascript!):
Rus</li>
then, lang = request.querystring("lang")
Other approach is create text files (rus.asp, esp.asp,...) with variables (txt_title="Titulo", txt_button_yes="Si",...) and includes one file or another depending of language. Include files is fast than query to database.
Another thing: is not a good idea create includes with .inc beacuse the code might be visible. Use .asp instead.

Document.getElementById("LanguageSelected").innerText looks like client side Javascript. ASP is server side code, it's executed when the page is served, so you'll need to populate your variable Lang either with a querystring value or a form submission and retrieve it with something like Lang = request("Lang")

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!

I have a trouble getting an element from HTML page

what I do is I navigate to a site then I want to find an element called "jobId"
Const PAGE_LOADED = 4
Set objIE = CreateObject("InternetExplorer.Application")
Call objIE.Navigate("http://172.25.25.32:8090/")
objIE.Visible = True
Do Until objIE.ReadyState = PAGE_LOADED : Call WScript.Sleep(100) : Loop
objIE.Document.all.Username.Value = "ashishgi"
objIE.Document.all.Password.Value = "apac2015#"
If Err.Number <> 0 Then
msgbox "Error: " & err.Description
End If
Call objIE.Document.all.gaia_loginform.submit
Set objIE = Nothing
when I use ViewSource this is the elements I want to use :
input type="submit" name="btnsubmit" value="Login" id="logincaption" class="button" style="color:#565656"
So, you're trying to click submit? Your explanation is difficult to interpret what you want the script to do. Based on the element you displayed on your sample is a submit button. It's been a while since I automated a form but I believe you want something to this effect.
objIE.Document.getElementsById("Username").Value = "ashishgi"
objIE.Document.getElementsById("Password").Value = "apac2015#"
' If you want to use the button by name
objIE.Document.all.item("btnsubmit").click
' If you want to use the button by ID
objIE.Document.getElementsById("logincaption").click
'if that fails, make it an object like this and action the click.
Set Submit = objIE.Document.getElementsById("logincaption")
submit.click

VBScript/Classic ASP find lines within csv file

I have a simple script which opens a csv file then (this worked fine with 1 result) iterates through to find an "x" and "y" value, then output those values. Problem is, with 3 different results I can only ever get it to display the final set of values. I know this is likely a simple for each/while syntax solution but as daft as it sounds, I can never get a for each to work correctly. The code I have currently (as everything else broke badly and left it as it originally worked):
dim fs,objTextFile
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile("Colour.csv")
Do while NOT objTextFile.AtEndOfStream
arrStr = split(objTextFile.ReadLine,",")
if arrStr(0) = "x" Then
Xval = arrStr(1)
end if
if arrStr(0) = "y" Then
Yval = arrStr(1)
end if
Loop
response.write(Xval & "<br />")
response.write(Yval & "<br />")
objTextFile.Close
set objTextFile = Nothing
set fs = Nothing
And a sample from the CSV file:
,
,
,
Name,Measurement #1
Date,11/07/2014
Time,10:52
T_int [ms],1439
,
Lv [cd/sqm],242.2
Le [W/(sr*sqm)] (380-780nm),0.8
CCT [K],5015
DWl [nm],568.8
PE [%],12.7
,
Chrom. Coord.,
x,0.3463
y,0.3625
u',0.2081
v',0.4901
------
,
,
,
Name,Measurement #2
Date,11/07/2014
Time,10:52
T_int [ms],1439
,
Lv [cd/sqm],242.2
Le [W/(sr*sqm)] (380-780nm),0.8
CCT [K],5015
DWl [nm],568.8
PE [%],12.7
,
Chrom. Coord.,
x,0.4724
y,0.0765
u',0.2081
v',0.4901
It's each of the x and y values (in Chrom. Coord) I need to retrieve then place them each in their own variable. How would I achieve this?
Move these lines into your Do loop:
response.write(Xval & "<br />")
response.write(Yval & "<br />")

How can I strip the element using vbscript and display in message box?

I would like to find the price with 2 year contract and display it in a message box. Sofar I have:
Dim MyPage
Dim Price
Set MyPage=CreateObject("Microsoft.XMLDOM")
MyPage.load("http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723")
Wscript.Sleep 2000
Set Price = MyPage.getElementsByTagName("span")
For Each Elem In Price
MsgBox(Elem.firstChild.nodeValue)
Next
I understand that I am completely wrong, but I don't even know where to start. I love writing simple programs like this, but I just need help getting started. Any ideas will help!
Here a better version, uses the HTMLFile object
Dim HTMLDoc, XML, URL, table
Set HTMLDoc = CreateObject("HTMLFile")
Set XML = CreateObject("MSXML2.XMLHTTP")
URL = "http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723"
With XML
.Open "GET", URL, False
.Send
HTMLDoc.Write .responseText
End With
Set spans = HTMLDoc.getElementsByTagName("span")
for each span in spans
WScript.Echo span.innerHTML
next
'=><SPAN>Set Location</SPAN>
'=>Set Location
'=><SPAN>Submit</SPAN>
'=>Submit
'=>Connect with us
the control you use is for reading XML documents, you need something like this
'Create an xmlhttp object, the string depends on the version that is installed
'on your pc could eg also be "Msxml2.ServerXMLHTTP.5.0"
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "GET", "http://admin:pasword#10.0.0.2/doc/ppp.htm", False
xmlhttp.Send
text=xmlhttp.responseText
wscript.echo text
Set xmlhttp = Nothing
Run a search in your registry for XMLHTTP to get the right string/version for the identifier.
To get the tag from the html you can use the following
text = "blabla <span>this is what i need</span> bla bla<span>second item</span> end"
function getElementsByTagName(sTextToSeachIn, tag)
answer = ""
separator = ""
set oRegExpre = new RegExp
with oRegExpre
.IgnoreCase = true
.Global = true
.MultiLine = True
.Pattern = "<" & tag & ">(.*?)</" & tag & ">"
end with
set oColMatches = oRegExpre.Execute(sTextToSeachIn)
for each match in oColMatches
answer = answer & separator & match.subMatches(0)
separator = "|" 'use something that's not in the spancontents
next
if separator <> "" then
getElementsByTagName = split(answer, separator)
else
getElementsByTagName = array()
end if
end function
for each tag in getElementsByTagName(text, "span")
wscript.echo tag
next
'=>this is what i need
'=>second item
There are better techniques and certainly better languages than vbscript to do this, i suggest to take a look at Ruby which exels in such things.
Alex, in response to your comment about getting a cookie and running a javascript in HTMLFile, here a ruby script i found, hopes it helps you at some point, it reads in a page, passes it to the HTLMFile object and in that DOM executes a remote javascript file. It also gives you an idea of the combined power of activeX and Ruby.
require "win32ole"
$jsxpath_uri = "http://svn.coderepos.org/share/lang/javascript/javascript-xpath/trunk/release/javascript-xpath-latest-cmp.js"
uri, xpath = "http://gist.github.com/gists", "//div[#class='info']/span/a"
http = WIN32OLE.new('MSXML2.XMLHTTP')
http.Open "GET", uri, false
http.Send
text = http.responseText
dom = WIN32OLE.new("htmlfile")
dom.Write(text)
dom.parentWindow.eval(open($jsxpath_uri){|f| f.read })
items = dom.evaluate(xpath, dom, nil, 7, nil)
len = items.snapshotLength
(0...len).each do |i|
item = items.snapshotItem(i)
puts item.innerHTML
end

Update a database record without refreshing a page. ASP VBscript

This requests for help follows a poorly worded request for help earlier (apologies).
I have two asp/vbscript pages. The second page (containing only <% vbscript %> is called when a form on the first page is submitted.
The code on the second page causes an update to a database record and is as follows (the fields are populated from querystring variables.):
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "dsn=xxx;uid=xxx;password=xxx;"
SQLString = "UPDATE dbo_tbl_printing_tempstore SET " & fieldPrefix & "has_text1 = 'YES', " & fieldPrefix & "text = '" & fieldUpdate & "' WHERE id = " & tempid & ""
MyConn.Execute(SQLString)
MyConn.Close
Set MyConn = Nothing
All the form submit does is to cause the database update to happen - nothing else.
The page then response.redirects back to the calling page. This causes a refresh and lot of data on the first page to be lost - this is what I'm trying to avoid.
Can someone please tell me how I can carry out the update without leaving and then refreshing the calling page please? I've been told Ajax can do this but I have no experience at all of using it.
Many thanks
First of all, change your code to use parameters so you will be protected against SQL Injection attack:
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "dsn=xxx;uid=xxx;password=xxx;"
SQLString = "UPDATE dbo_tbl_printing_tempstore SET " & fieldPrefix & "has_text1 = 'YES', " & fieldPrefix & "text = ? WHERE id = ?"
Set MyCommand = Server.CreateObject("ADODB.Command")
Set MyCommand.ActiveConnection = MyConn
MyCommand.CommandType = 1
MyCommand.CommandText = SQLString
MyCommand.Parameters.Append(MyCommand.CreateParameter("#text", 200, 1, 0, fieldUpdate))
MyCommand.Parameters.Append(MyCommand.CreateParameter("#id", 3, 1, 0, tempid))
MyCommand.Execute()
MyConn.Close
Set MyCommand = Nothing
Set MyConn = Nothing
Having this, the next step is adding hidden frame in the first page:
<iframe id="MyFrame" name="MyFrame" style="display:none;"></iframe>
And finally simply add target to your <form> tag like this:
<form action="SecondPage.asp" target="MyFrame">
That's it... now the form will be submitted "inside" the hidden frame, will still trigger the database update and won't cause any refresh.

Resources