mixing asp and html codes - html

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
%>

Related

How to display Success message after clicking Save in HTML page

I have an html form containing this code which runs on IIS:
<form action="http://server1/data.asp" method="POST">
Your Name: <input name="name" type="text">
<input value="Save" type="submit">
</form>
After user submits this form it runs the code in data.asp which displays a Success message with code to write to a text file (file1.txt):
Success!
<%
Dim fso
Dim tst
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Set tst = fso.OpenTextFile("C:\Inetpub\wwwroot\data\file1.txt", 8, true)
tst.writeline "Name = " & Request.Form("name")
tst.writeline "" & Request.Form("")
tst.close
Set tst = Nothing
Set fso = Nothing
%>
This works but it just shows a page with Success! and stays there. What I'd really like is:
After user submits the form by clicking Save I'd like a little message box that says "Success" and remain on the same html page or go to a url I specify within the code.
I don't want to use Java or 3rd party extensions. This is an HTML page that runs the data.asp page on submit and I'd like it to stay that way if possible.
So how would I modify the html and/or data.asp page to do this?
You can do like this:
Response.Redirect "https://www.yoursite.com/thankyou.asp"
Instead of returning "Success!" as your response content you can return the form content you have above, this way you can use the same file for the GET and POST methods, with an condition on your asp logic to execute only on POST requests and return a success alert when the code is excuted successfully.
<form action="http://server1/index.asp" method="POST">
Your Name: <input name="name" type="text">
<input value="Save" type="submit">
</form>
<%
If (Request.Method = "POST") Then
Dim fso
Dim tst
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Set tst = fso.OpenTextFile("C:\Inetpub\wwwroot\data\file1.txt", 8, true)
tst.writeline "Name = " & Request.Form("name")
tst.writeline "" & Request.Form("")
tst.close
Set tst = Nothing
Set fso = Nothing
response.write ("<script>alert('Success!');</script>")
End If
%>
Found the solution by adding an extra page to the mix (redir2form.asp):
in data.asp:
<%
Dim fso
Dim tst
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Set tst = fso.OpenTextFile("C:\Inetpub\wwwroot\data\file1.txt", 8, true)
tst.writeline "Name = " & Request.Form("name")
tst.writeline "" & Request.Form("")
tst.close
Set tst = Nothing
Set fso = Nothing
%>
Response.Redirect "http://server1/redir2form.asp"
in redir2form.asp:
<%
response.write ("<script>alert('Success!');</script>")
%>
<meta http-equiv="refresh" content="1; url=http://server1/index.htm" />
Index.htm has the form action going to the data.asp page.
Not as neat as it could be but that's how I got it to work. After a user submits the form he gets the "Success!" message box and once they click OK they're redirected back to the form page.

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

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
%>

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.

asp form question

I have this form
<form action="http://www.mysite.com/asp/formd.asp" method="post" target="_blank">
so the asp looks like below,
it opens a new window where ot says "send ok"
my question is how and where can I contro/define the style of this new window i.e background fonts color etc
thanks
the ASP code:
<%# Language=VBScript %>
<%
Dim txtbody
Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.To = "mail#mail.com"
objCDO.From = "digital#adinet.com"
objCDO.Subject = "* *Formu enviado desde web * *"
txtbody = ""
for a = 1 to Request.Form.Count
txtbody = txtbody & Request.Form.Key(a) & " = " & Request.Form(a) & chr(10) & chr(13)
next
for a = 1 to Request.QueryString.Count
txtbody = txtbody & Request.QueryString.Key(a) & " = " & Request.QueryString(a) & chr(10) & chr(13)
next
txtbody = txtbody & "*******-----------------******"
objCDO.Body = txtbody
objCDO.Send
Response.Write "send = Ok"
%>
If you'd like to have a page or message that's more meaningful, consider replacing this
Response.Write "send = Ok"
with this:
Response.Redirect "email-thank-you.htm" 'or .asp, whatever you like.
Then go make your new page email-thank-you.htm as decorated and styled as nicely as you can. This helps by having your email logic contained in one page or function, and separate from the nice page. If something happened, i.e. the email server was unavailable, or perhaps the email address was malformed/missing, you could write that back to the original page.
If I understand what you're doing correctly, you should actually create a static form called emailForm.asp with your desired styling. Have it read the querystring that you are passing and and place the values in the fields. Put on link on your current page in order to pop this page up.
Your final line, Response.Write "send = Ok" is being output as a badly-formed html page.
I'd recommend you structure your page as follows:
<%# Language=VBScript %>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Add header info, including links to style sheets -->
</head>
<body>
<%
'Your CDO code goes here
objCDO.Send
if err.number > 0 then
response.write "<p class='error'>Error: " & err.number & " - " & err.message & "</p>"
else
Response.write "<p class='ok'>Sent OK</p>"
end if
%>
</body>
</html>
This will render a full html page that you can style properly (and will also no presume that the email sent OK!).