Using Classic Asp variable to update CSS - html

I have a classic ASP page. On this page I need to hide a table based on whether the database that fills that table returns any results. If the table is empty then the header is hidden. <table> doesn't have a visible or display element thus I am wrapping it in a <div>. However when the page executes the css isn't applied.
.hideDiv {
display: <%=vis%>;
}
<div class="hideDiv">
<table>
<!-- Table elements -->
<%
' Other code
If count > 0 Then
vis = "block"
Else
vis = "none"
End If
' The vis variable is not updated past this point
%>
</table>
</div>

I think you have a few options.
Here's an old fashioned method.
Option 1:
Instead of having your CSS determine the Show or Hide of your table, have the If Count > 0 do the work server side .
If count > 0 Then
Response.Write("<table>" & vbCrLf)
'# Do you Table Tags and your code here.
Response.Write("</table>" & vbCrLf)
End If
If you must write CSS for your script you typically need to write the script twice, so you can have your CSS embedded in your header correctly.
Option 2:
Placed in header .
<%
Dim vis
If count > 0 Then
vis = "block"
Else
vis = "none"
End If
Response.Write("<style type=""text/css"">" & vbCrLf)
Response.Write(" .hideDiv {" & vbCrLf)
Response.Write(" display: "&vis&";" & vbCrLf)
Response.Write("}" & vbCrLf)
Response.Write("</style>" & vbCrLf)
%>
Then you can place your table in the body.
<div class="hideDiv">
<table>
<!-- Table elements -->
</table>
</div>
Option 3:
You can Inline your CSS and make it work. Or at least it should as long as your code sets the vis.
<%
Dim vis
If count > 0 Then
vis = "block"
Else
vis = "none"
End If
%>
<div style="display:<%=vis%>;">
<table>
<!-- Table elements -->
</table>
</div>
Often times in ASP Classic we need to write a small script to check if our table data is there. Remember to follow the left to right, top to bottom if you're not placing things in function or sub calls.
The count > 0 needs to trigger the building of your CSS so it can include the vis to your <Div> element.
If you're getting your Count value after running your SQL then you might need to setup that second script to test if you have data for your table then build your CSS.
Example:
Function MyCount()
Dim Count
Count = 0
SQL = SELECT Top 1 ID FROM Table WHERE FIELD1 Is Not NULL
'# blah
If rs.EOF=False Then
count = 1
End If
MyCount = count
End Function
We then we can mix the examples above to only trigger when we have a table to show.
<header>
<%
If MyCount() = 1 Then
Dim vis
vis = "block"
Else
vis = "none"
End If
%>
</header>
In the body you could then use something like the following.
<div style="display:<%=vis%>;">
<table>
<!-- Table elements -->
</table>
</div>
In your post you are actually calling the <%=vis%> before you set it.
Top to Bottom, Left to Right, reorder your code.

You should put below code at the top, then you test again:
If count > 0 Then
vis = "block"
Else
vis = "none"
End If

Below codes work well in my computer
<%
' Other code
If count > 0 Then
vis = "block"
Else
vis = "none"
End If
' The vis variable is not updated past this point
%>
.hideDiv {
display: <%=vis%>;
}
<div class="hideDiv">
<table>
<!-- Table elements -->
</table>
</div>

Related

How to extract something between <!-- --> using VBA?

I'm trying to scrape a page using VBA. I know how to get elements by id class and tag names. But now I have come across this Tag
<!-- <b>IE CODE : 3407004044</b> -->
Now after searching on the internet I know that this is a comment in the HTML, but what I'm unable to find is what is the tag name of this element ,if it qualifies as a tag at all. Should I use
documnet.getelementsbytagname("!") ?
If not, how else can I extract these comments ?
EDIT:
I have a bunch of these td elements within tr elements and I want to extract IE Code : 3407004044
Below is a larger set of HTML code:
<tr align="left">
<td width="50%" class="subhead1">
' this is the part that I want to extract
<!-- <b>IE CODE : 3108011111</b> -->
</td>
<td rowspan="9" valign="top">
<span id="datalist1_ctl00_lbl_p"></span>
</td>
</tr>
Thanks!
Give it a try like this, it works if you fix it a bit further:
Option Explicit
Public Sub TestMe()
Dim myString As String
Dim cnt As Long
Dim myArr As Variant
myString = "<!-- <b>IE CODE : Koj sega e</b> -->blas<hr>My Website " & _
"is here<B><B><B><!-- <b>IE CODE : nomer </b> -->" & _
"is here<B><B><B><!-- <b>IE CODE : 1? </b> -->"
myString = Replace(myString, "-->", "<!--")
myArr = Split(myString, "<!--")
For cnt = LBound(myArr) To UBound(myArr)
If cnt Mod 2 = 1 Then Debug.Print myArr(cnt)
Next cnt
End Sub
This is what you get:
<b>IE CODE : Koj sega e</b>
<b>IE CODE : nomer </b>
<b>IE CODE : 1? </b>
The idea is the following:
Replace the --> with <!--
Split the input by <!--
Take every second value from the array
There are some possible scenarios, where it will not work, e.g. if you have --> or <!-- written somewhere within the text, but in the general case it should be ok.
You can use XPath:
substring-before(substring-after(//tr//comment(), "<b>"), "</b>")
to get required data

How to get the Text of a Div Class and Place inside a Label?

I'm trying to get the "valor" that is inside the site div from Poloniex to put inside a Label every 1 second using Timer after load site completely.
My Completely Code:
https://pastebin.com/QrcuWnxf
Div:
<div class="info">valor</div>
I have found that on the page there are several "info" classes...
This code worked, but it brought another result than expected ...
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("Div")
For Each curElement As HtmlElement In theElementCollection
If curElement.OuterHtml.Contains("info") Then
Variable1 = (curElement.GetAttribute("InnerText"))
End If
Next
Label1.Text = Variable1
The result was the value of this Div that also has the "info" class.
<div class="msg"><div class="info">OMG/BTC and OMG/ETH OmiseGO markets added</div>
Change the second try:
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element.GetAttribute("className") = "lastPrice" Then
For Each Element1 As HtmlElement In Element.GetElementsByTagName("div")
If Element1.GetAttribute("className") = "info" Then
Dim Variable1 as String = Element1.InnerText
End If
Next
End If
Next
Yes, you have to use className for class attribute.
This code is verified against the following HTML: This also have more than 1 info class.
<html>
<body>
<div class="firstPrice">
<div class="name">First Price</div>
<div class="info">11650.00</div>
</div>
<div class="lastPrice">
<div class="name">Last Price</div>
<div class="info">11650.00</div>
</div>
</body>
</html>

Calling a vbscript funciton to update a SQL table from an HTML form generated on an asp-classic page

Having a hard time with updating a SQL table from an HTML form generated on an asp-classic page. The page dynamically creates a table based on a SQL query; I've added a form for the purpose of editing certain fields displayed in the table but have been unable to trigger the function (i.e. onclick) that the form button calls using the code below. I've tried multiple variations of the syntax used for calling the function via VBscript and am missing something entirely. I'm wondering if I'm trying something that can't be done in the asp-classic environment due to the server side code being used and the limitations that go with that (as well as my trying to use the same page to generate the results). I know the default behavior for the form is to refresh the page if I don't specify a different page. In the code below, I'm just trying to confirm the behavior before I add the SQL update statement to the function. (I'm typically only retrieving or inserting data for a web site - very little experience updating the data between a single page like this.)
<br>
<form method="POST" id="form" action="">
<table>
<tr>
<td>
<select name="state">
<%
'populate the dropdown selection for the state within the form
do until rs.eof
For each x in rs.fields
response.write "<option value=" & x.Value & ">" & x.Value & "</option>" & vbNewLine
next
rs.movenext
loop
%>
</select>
</td>
<td>
<select name="ps">
<option value="blank"></option>
<option value="prime">Primary</option>
<option value="secondary">Secondary</option>
</select>
</td>
<td>
<input name="emp_num" size="5" maxlength="5" rows="1" ></textarea>
</td>
<td>
<input type="submit" onclick="Go" value="Update record"/>
</td>
</tr>
</table>
</form>
<%
function Go() {
msgbox "test"
//additional SQL code would be included here once the function actually works
}
%>
<%
'close the DB connection
dbconn.close
%>
</body>
</html>
You're trying to mix server-side code with client-side code, and It Don't Work Like ThatTM.
Here's a very rough outline of how you could get information from the user, process it, and display it, all on the same asp page. Note the utter lack of JavaScript. :)
<html>
<head>
<title>My Page</title>
<link rel="stylesheet" type="text/css" href="css/stylestuff.css">
<%
Dim rs, sql, conn
Dim mylist, myval1, myval2, i
myval1 = Request.Form("val1")
myval2 = Request.Form("val2")
'- If the form hasn't been submitted yet, myval1&2 will be the special value Empty,
'- but you can treat them as a blank string ("") for all intents and purposes.
If Validate(myval1, myval2) = True and myval1 & myval2 <> "" Then
DoStuffWith myval1, myval2
End If
%>
</head>
<body>
<h1>My Page</h1>
<form method="post" action="">
<p>Select one: <select name="val1" size="1">
<%
ListStuff mylist
For i = 0 to UBound(mylist,1)
response.write "<option value='" & mylist(0,i) & "'"
If myval & "" = mylist(0,i) & "" Then response.write " selected"
response.write ">" & mylist(1,i) & "</option>"
Next
%>
</select></p>
<p>Write something here:
<input type="text" name="val2" value="<%=myval2%>" size="10"></p>
<p><input type="submit" name="btn" value="Submit"></p>
</form>
<%
If myval1 & myval2 <> "" Then
DisplayStuff myval1, myval2
End If
%>
</body>
</html>
<%
Sub ListStuff(L)
sql = "Select [...] From [...] Where [...]"
Set rs = Server.Createobject("ADODB.Recordset")
rs.Open sql, "connection string (or previously-opened connection object)", 1,2
If Not rs.EOF Then
L = rs.GetRows
Else
Redim L(1,0)
L(0,0) = 0 : L(1,0) = "missing!"
End If
rs.Close
Set rs = Nothing
End Sub
'---------------------------------
Function Validate(v1,v2)
dim f
f = True
If Not Isnumeric(v1) Then f = False
If Len(v2) > 10 Then f = False
Validate = f
End Function
'---------------------------------
Sub DoStuffWith(v1,v2)
'- table-creation (or whatever) code goes here
End Sub
'---------------------------------
Sub DisplayStuff(v1,v2)
'- display code goes here
End Sub
%>

mixing asp and html codes

i have this code which contains html and asp code
<%for each x in rs.Fields%>
<%IF (x.name="ID") THEN%>
<%dim i
i=x.value%>
<td><a href="form7.asp?id="+<%i%>>
<%Response.Write(x.value)%><a/>
i want to use the i variable inside the html code
or another example
<%id=request("id")%>
<%=id%>
<tr>
<th>Name:</th>
<td><input name="n"></input></td>
i want to use id in the input tag in the value as value=id
how to do that ? can someone help me please ?
First, a basic ASP design principle: try to minimize the switching between HTML context and ASP (or really, VBScript) context on a page, for reasons of performance as well as readability.
Following that principle in your latter snippet, I would use Response.Write to emit the necessary HTML as follows:
<%
id=request("id")
Response.Write "<tr><th>Name:</th><td><input name=""n"" value=" & id & "></input></td></tr>"
%>
All you're doing is supplying the VALUE attribute of the INPUT tag.
Fixed:
<%
dim i
for each x in rs.Fields
IF (x.name="ID") THEN
i=x.value
response.write("<td><a href='form7.asp?id=" & i & "'>"
response.write(x.value) & "<a/>"
'not sure if you want a closing TD here
response.write("</td>") & vbCrLf
END IF
next
%>

How to display the contents of an arraylist in a drop down box

I have a sql statement that pulls infomration about tagnum's for individual pidm's. Every pidm can have multiple tagnum's so I am dropping the information into an arraylist. I want to display the contents of this arraylist in a dropdown box on a html page.
Here is the code for the arraylist:
<table style="border:transparent" style="width:100%">
<tr>
<td style ="width: 300px;">
<select style="width:150px;"tabindex="5" name="Tag">
<option></option>
<%} rscheck.close();
ResultSet rsTagCheck = stmt.executeQuery("SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS, PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'");
while (rsTagCheck.next()){
ArrayList<String> myTag = new ArrayList<String>();
myTag.add(rsTagCheck.getString("XKRPRMT_TAG"));
%>
<option><%= myTag.get(0) %></option>
</select>
</td>
I can get the first element to show in the drop down box, but anything after that show an outofbounds exception. I want to know how to display ALL of the information in the arraylist.
#Pointy I did that and all I got was this:
It put the first one in there, but the rest would not populate!!
There's no reason to create the array list at all.
while (rsTagCheck.next()) {
%>
<option><%= rsTagCheck.getString("XKRPRMT_TAG") %></option>
<%
}
edit — of course in practice you should be careful about what those strings might contain. If the strings come from some sort of user input, you shouldn't be just dumping them unwashed into the HTML. That's a whole other subject however.
Don't use scriptlets, use jstl tags and in this case
<c:forEach var="myTag" items="${rsTagCheck}">
and
<c:out value="${myTag.getString('XKRPRMT_TAG')}" />
Actually looking again at your code, I would not put the db query in a scriptlet! DB access should not be done here, pass the resultsets to jsp from servlet, and loop through data using jstl.