classic asp application variables in if...then...else statement - vbscript

If I store a variable in the global asa Application("emergency") = "Emergency" I can echo "Emergency" by writing <% = Application("emergency") %>. How can I do that in an If... Then ... Else statement?
The code that I'm working on ...
<% If date()<= <= #3/29/2017# Then response.write("<div style=""margin:30px 0 0""><!-- #INCLUDE FILE=""file.asp"" --></div>") Else response.write(" = Application("emergency") ") %>
produces an error. Little help. Thanks.

It produces an error because this is not a valid string:
" = Application("emergency") "
Notice the quotes within quotes.
Instead of trying to put code in a string, just call the code:
response.write(Application("emergency"))

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!

Issue running Classic ASP written in VBScript on Windows 2012 with IIS 8

This is a very peculiar issue. I have a 2012 Server running IIS 8 with support for classic ASP installed. I am building a comma separated string from a form. I then am retrieving this string from a table and want to split on the commas.
First, when I build the string and submit it to the DB (SQL Express 2014), something is adding a space after each comma even though there is no space in the code.
Second, when I return the string and attempt to split on the comma, it doesn't do anything; the ubound method returns -1... For testing purposes, I hand built an array and this has the same behavior.
Code that builds the csv string:
If fieldName = "txt_EnvironmentType" then
strTempEnvCSV = strTempEnvCSV & fieldValue & ","
End If
Test code for split:
txtEnvironmentType = "This,Is,A,Test,String"
If txtEnvironmentType <> "" then
response.write(txtEnvironmentType)
array = split(txtEnvironmentType,",")
l = ubound(array)
response.write("<br>array is " & l & " long")
For i = LBound(array) to UBound(array)
response.write("<br>" & array(i))
Next
End If
The Above test code returns the following to the browser:
This,Is,A,Test,String
array is -1 long
Am I missing something?
Thanks!
For the mysterious spaces, add a TRIM() to make sure you aren't starting with spaces:
If fieldName = "txt_EnvironmentType" then
strTempEnvCSV = strTempEnvCSV & TRIM(fieldValue) & ","
End If
This ran (for your second issue) for me - the only change I made was to dim the array variable and name it something other than "array"
<%
dim arrMine
txtEnvironmentType = "This,Is,A,Test,String"
If txtEnvironmentType <> "" then
response.write(txtEnvironmentType)
arrMine = split(txtEnvironmentType,",")
l = ubound(arrMine)
response.write("<br>arrMine is " & l & " long")
For i = LBound(arrMine) to UBound(arrMine)
response.write("<br>" & arrMine(i))
Next
End If
%>

VBScript For Loop not executing if statement?

Not exactly sure how to search for this issue I have with VBScript, so please forgive if this is a duplicate.
I have a function that prints pagination on a page that takes 2 parameters, totalpgs and active. My issue is that the active page only applies the style on the link when the variable pg is NOT set. I'm pretty sure this is a logic issue but I've been staring at this for 7 days now.
Here is the function:
'======= Print Pagination links
function print_pagination (totalpgs,active)
p = "<div class=""row"">"
p = p & "<div class=""pagination pagination-right"">"
p = p & "<ul>"
for x = 1 to totalpgs
if x = active then
li = "<li class=""active"">" '<- Set class for current page
else
li = "<li>" '<- else regular link
end if
p = p & li & "<a href='?pg="& x &"'>"& x &"</a></li>"
next
p = p & "</ul></div></div>"
print_pagination = p
end function
Here is the code on the page:
pg = request.querystring("pg")
if pg = "" then
pg = 1
end if
<%=print_pagination(totalpages,pg) %>
Here is the desired result:
I want the current <li> to contain a class called active.
This is what's happening:
The active class never gets applied to the <li> tag. When the url contains the get variable of pg, no active class is assigned. When it does not, then it's applied to the first link.
Change your line that does the comparison to this:
if cstr(x) = cstr(active) then

Simple Regex Replace Visual Script

I have the following code
<%
txt = ""+(rs_email.Fields.Item("g_email_body").Value)+""
Set objReg = CreateObject("vbscript.regexp")
objReg.Pattern = "[activate]"
activate = (objReg.Replace(txt,"http://www.gamtool.com/activate.asp?id="+(Recordset1.Fields.Item("g_userbase_id").Value)+""))
%>
When I run the code I get an error on the last line
activate = (objReg.Replace(txt,"http://www.gamtool.com/activate.asp?id="+(Recordset1.Fields.Item("g_userbase_id").Value)+""))
%>
any ideas why I am getting the error?
I have updated the code above to the following:
<%
Set regEx = New RegExp
regEx.Global = true
regEx.IgnoreCase = True
regEx.Pattern = "\[activate\]"
strText = ""+(rs_email.Fields.Item("g_email_body").Value)+""
activate = regEx.Replace(strText, ""+(Recordset1.Fields.Item("g_userbase_id").Value)+"")
%>
If I change the ""+(Recordset1.Fields.Item("g_userbase_id").Value)+"") to any value but insert the value static then it works.
Thanks
#Gerald Ferreira: Instead of using +, use & to concatenate. You're getting the type mismatch error because VB Script thinks you're trying to add which, of course, isn't the same as concatenating.

end of line error with classic asp

I have a do a project for school with classic asp which I ave never used before.
I keep getting this syntax error:
Expected end of statement
set Y=server.CreateObject("ADODB.Connection");
---------------------------------------------^
The asp code i'm trying to execute is:
<%
set Y=server.CreateObject("ADODB.Connection")
X = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ= " & Server.MapPath(".\account.mdb")Y.open X
user=request.Form("username")
set useraccount=Y.Execute("select * from logintable where username='" & user & "'")
if useraccount.eof then
Response.Redirect("41697hw1noaccount.htm");
else if useraccount.fields[0].passwd == request.Form("pwd") then
Response.Redirect("41697hw1wrongpasswd.htm")
else
Response.Write("<h1>Welcome, " & useraccount.fields[0].fullname & "</h1>")
end if
%>
Thanks so much for your help!
I am not familiar with VBScript, but I think you should not put a ';' at the end of your statement. I think removing the semi-colon will help.
In vbscript the explicit "end-of-line " character is a colon ":"
It is not required in vbscript unless you want to put more than one statement on a physical line.
for example--
Dim myVariable : myVariable = "some string"

Resources