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

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!

Related

Asp-classic Vbscript webpage language switcher into variable

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")

ASP script tags involving buttons within HTML

Have a question regarding scripts I've never worked on before. I am trying to get rid of two buttons that seem to be involved in the same script. Inside the body tags, I try to get rid of anything inside of the script, it'll break the page. Tried to do some research on it, but no luck.
This is inside the scripts section
HI, this is the section inside of the body tags
catid = Request.QueryString("id")
sub productInfo(connObj,category)
sqlCustomer = "SELECT * FROM qryProdsCategory WHERE ccategory = '" & Cint(category) & "'"
Set rs = Server.CreateObject ("adodb.Recordset")
rs.Open sqlCustomer, dbc, adOpenDynamic, adLockOptimistic, adCmdText
if not rs.EOF then
if Session("sort")="0" then
rs.sort = "cname ASC"
end if
while not rs.EOF
If rs("stock")="1" then
Response.Write "<form action="&q&Application("secureurl")&"/cart/view-cart.asp"&q&" method="&q&"POST"&q&" name=form"&i&">"
Response.Write "<a href=""product.asp?id=" & rs("catalogID") & ""
Response.Write "" & rs("catalogID") & "" & rs("manModNum") & "</font></td><td width=""18%"" rowspan=""2"">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fproductid"&q&" value="&q & rs("catalogID")& q&">"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fquantity"&q&" value=1>"
Response.Write "<input type="&q&"hidden"&q&" name="&q&"fcat"&q&" value=" & rs("ccategory") & ">"
If rs("stock")="" then
Response.Write "<button class=""btn"" TYPE=""btn"" style=""background-color: #cb0000;color: #fff;"">.</button> </form>"
Else
Response.Write "<button "">ADD</button></form>"
End If
End If
rs.MoveNext
wend
else
Response.Write " <P><Center><font size=""2""><h3>Sorry, but products information for the category you have chosen is not available at this moment. Please check back soon!</H3></font></center>"
catname = "Error"
end if
end sub
%>
<!-- INSIDE BODY TAGS BELOW -->
<!-- Start Profile -->
<div class="span3">
<img src="img/team/profile1.jpg">
<div class="productSelection">
<div class="ProductTitle">
<strong style="font-size:16px;">LOREM IPSUM</strong>
</div>
<h6>LOREMIPSUM</h6>
<h6>LOREM IPSUM</h6>
<%
call openConn()
call productInfo(dbc,catid)
%>
</div>
</div>
<!-- End Profile -->
This is some bad code. The code is closing the form tag twice.
Since you say you only need one button, and since the form tag is either being closed twice or not at all, I think that one of the Response.Write statements needs to be in the other If branch.
'DONT TOUCH THESE BELOW
If rs("stock")="" then
Response.Write "<button class=""btn"" TYPE=""btn"" style=""background-color: #cb0000;color: #fff;"">ADD TO CART</button> </form>"
Else
Response.Write "<button "">ADD TO CART</button></form>"
'DONT TOUCH THESE ABOVE
End If

Illegal Syntax. Expected valid start name character

Not sure whats gone wrong here, not very good at this stuff so apologise in advance. Ive got the below but faulting when trying to run. Not sure whats gone wrong with this part as used to work before on an older server and migrated the data across.
<%
rs.Close
if request.form () <> "" then
varSearch = replace(request.form("T1"),"*","%")
varSearch = replace(varSearch," ","%")
sql = "SELECT * FROM [Cards] WHERE [CardTypeID] = 'C' AND [Name] LIKE '%" & varSearch & "%'
ORDER BY [Name]"
rs.Source = sql
rs.Open
%>
Thanks.
request.form() - you're not capturing anything
<%
rs.Close
if request.form ("T1") <> "" then
varSearch = replace(request.form("T1"),"*","%")
varSearch = replace(varSearch," ","%")
sql = "SELECT * FROM [Cards] WHERE [CardTypeID] = 'C' AND [Name] LIKE '%" & varSearch & "%'
ORDER BY [Name]"
rs.Source = sql
rs.Open
%>

Asp OpenDB server not found

Get 505 error on this command on my asp file due to this code can you tell me fix of this please i will be thankful to you.
<%
OpenDB()
'Stmt = "SELECT * FROM articles WHERE status=1 "
Stmt = "SELECT * FROM articles, articleCategories WHERE articles.articleID=articleCategories.articleID AND categoryID=1 AND status=1"
set rsQuery=server.CreateObject("ADODB.Recordset")
rsQuery.Open Stmt, OpenDB, 1, 1
DO WHILE NOT rsQuery.EOF
wl "<div class=""listSub"">" & rsQuery("title") & "</div>"
rsQuery.MOVENEXT
LOOP
%>
First of all do a proper join:
SELECT * FROM articles join articleCategories
on articles.articleID=articleCategories.articleID
where TableName1.categoryID=1 AND TableName2.status=1
Because I do not know if both of your tables have any of those columns(categoryID,status) or just one but sure way to get an error is what you did.
Secon issue is how do you wrote your OpenDB sub. Need to see to tell you if issue is there or not.
And last but not least, do not do any fancy staff if you do not know how:
DO WHILE NOT rsQuery.EOF
response.write "<div class='"listSub"'><a href='articles.asp?article=" & rsQuery("articles.articleID") & "'>" & rsQuery("title") & "</a></div>"
rsQuery.MOVENEXT
LOOP

ASP - Loop while eof or bof - Select random product from idproduct but skip empty ids

I am trying to display a random product image and description from the access database, so i am selecting the highest idproduct then randomising a number between 1 and %highestid%, this is what i have so far....
IF frontpage = 1 then
SQLSTR = "SELECT idproduct AS prodtot FROM products order by idproduct desc"
Set objRS = Server.CreateObject("ADODB.Recordset")
SET objrs = oconn.execute(SQLSTR)
' Check result
Response.Write objRS("prodtot")
' attach
ntop = objRS("prodtot")
Randomize
' Generate random value between 1 and nTop .
nRandom= Int((nTop * Rnd) + 1)
sqlstr = "select * from products where idProduct = " & nRandom
response.Write"<br /><br />" & (sqlstr) & "<br /><br />"
'SET rs = oConn.execute(randomprod)
SET rs = oconn.execute(SQLSTR)
pranproddesc = rs("description")
response.Write(pranproddesc)
pranprodimg = rs("smallImageUrl")
end if
So far so good! But i have a problem, over time products have come and gone and I have alot of gaps in the %idproduct%, ive tried loop while rs.eof but it doesn't seem to do anythimg usefull, if anything at all. Just to clarify I have idproduct 1, 2, 5, 10, 11, 12 etc etc, so when it randomises idproduct3 it all goes up the spout! Can anyone help?
Thank you in advance! :)
Do the following:
IF frontpage = 1 then
Set objRS = Server.CreateObject("ADODB.Recordset")
SET objrs = oconn.execute(SQLSTR)
sqlSTR = "SELECT TOP 1 * FROM products ORDER BY NEWID()"
response.Write"<br /><br />" & (sqlstr) & "<br /><br />"
SET rs = oconn.execute(SQLSTR)
pranproddesc = rs("description")
response.Write(pranproddesc)
pranprodimg = rs("smallImageUrl")
end if
That sql will work in SQL Server:
Look at this page for SQL to return a random row for other databases:
http://www.petefreitag.com/item/466.cfm
Thanks, btw, I learnt something new figuring this out.
Rather than select a random ProductId, select a random row index from the recordset. That way you only have to hit the database once as well :)

Resources