顯示具有 ASP 標籤的文章。 顯示所有文章
顯示具有 ASP 標籤的文章。 顯示所有文章

2014年11月6日 星期四

ASP(Active Server Pages) 應用技巧篇


2014/11/07
1.取得host,並組成網址
<%
        '取得host,並檢查是否是https
if Request.ServerVariables("HTTPS")="on" then
str_url="https://" & Request.ServerVariables("HTTP_HOST")
else 'off
str_url="http://" & Request.ServerVariables("HTTP_HOST")
end if
Response.write(str_url)

        '========================
        '印出Request.ServerVariables所有資料
'for each x in Request.ServerVariables
'    response.write(x & "=" & Request.ServerVariables(x) & "<br>")
'next
%>

2014年10月17日 星期五

ASP 使用instr及Mid函數,從字串中擷取某一段字串


<%
dim str_temp1,SEHM_s,SEHM_e,str_SEHM,new_temp1

'假設str_temp1是我自己儲存在資料庫裡的資料,而我要擷取的是SEHM=10:00~12:00這段資料
str_temp1="TEMP=ABCD;SEHM=10:00~12:00;TEMP2=EFGH;"

'搜尋SEHM在字串中的起始位置
SEHM_s=instr(str_temp1,"SEHM")  '搜尋出來的位置是11

'instr(參數1 ,參數2 ,參數3)
'參數1:從哪一個位置,開始搜尋
'參數2: 此處指的是str_temp1
'參數3:要搜尋的字串
SEHM_e=instr(SEHM_s,str_temp1,";")+1  '搜尋出來的位置是27再加1等於28,加1是為了取得分號(;)

'Mid擷取字串的函數; Mid(str_temp1, 起始位置 ,(SEHM_e-SEHM_s))
str_SEHM=Mid(str_temp1,SEHM_s,(SEHM_e-SEHM_s))
Response.write("取得SEHM:" & str_SEHM & "<br/>")

new_SEHM="SEHM=09:00~10:00;" '假設是變更後的資料
'============================
'法1.直接取代原本的字串
new_temp1=Replace(str_temp1,str_SEHM,new_SEHM)
'============================
'法2.先將原本找到的字串資料替換成空白,在最後面插入新的資料
'new_temp1=Replace(str_temp1,str_SEHM,"")
'new_temp1=new_temp1 & new_SEHM
response.write(new_temp1 & "<br/>")
%>

2014年7月5日 星期六

ASP HtmlDecode 字串編碼轉換html

參考資料1:ASP Encode/Decode Functions

說明:當資料庫的資料儲存了換行的編碼,如:「 &#38;lt;br /&#38;gt;」(這是一個html換行的編碼)。那該如何轉換成html <br/>呢? 請看下列程式碼

程式碼
<%
Function HTMLDecode(sText)
    Dim I
    sText = Replace(sText, "&quot;", Chr(34))
    sText = Replace(sText, "&lt;"  , Chr(60)) '&lt 會被取代為 < 符號
    sText = Replace(sText, "&gt;"  , Chr(62)) '&gt 會被取代為 > 符號
    sText = Replace(sText, "&amp;" , Chr(38))
    sText = Replace(sText, "&nbsp;", Chr(32))
    For I = 1 to 255
        sText = Replace(sText, "&#" & I & ";", Chr(I)) '&#38 會被取代為 & 符號
    Next
    HTMLDecode = sText
End Function

Dim str_text
str_text="第一點&#38;lt;br /&#38;gt;第二點" '假設此變數為資料庫取出的資料
str_text=HTMLDecode(str_text) '呼叫第一次,此時回傳回來的值為「第一點&lt;br /&gt;第二點」
str_text=HTMLDecode(str_text) '呼叫第二次,此時回傳回來的值為「第一點<br />第二點」

Response.write str_text
%>

P.S 在傳統的ASP中沒有Server.HtmlDecode方法所以必須自行加入 HTMLDecode方法

2014年6月27日 星期五

ASP 使用JMail寄信

參考資料:http://www.ravs.ntct.edu.tw/know/show.asp?QUESTIONID=50

前置作業:
安裝JMail
1-1.先到Dimac Development下載 w3Jmail,下載前他會要您填寫資料,填寫完後即可下載,下載完後請先行安裝。
1-2.安裝完後,請到安裝的路徑(如 C:\Program Files (x86)\Dimac\w3JMail\ )底下複製 jmail.dll,請注意這裡將會有兩種狀況。狀況1:如果您的電腦是64bit請將檔案貼到C:\Windows\SysWOW64 ;狀況2:如果是32bit請將檔案貼到C:\Windows\System32。
1-3.請使用命令提示字元,下指令安裝JMail。
    (1)如果是32bit,請在命令提示字元執行regsvr32 jmail.dll
    (2)如果是64bit,請看下圖




程式碼:
Function JMailSend(MailTo,MailSubject,str_body,mailacc,mailpass,mailurl,mailcomp)
    Dim HTMLMailBody
    On Error Resume Next 
set jmail= server.CreateObject ("jmail.message")
jmail.Silent = true
jmail.Charset = "utf-8"

 '附加檔案
'filefullpath = Server.Mappath(".") & "\" & "test.doc"
'JMail.AddAttachment(filefullpath)

HTMLMailBody="<html><head><meta content=" & chr(34) & "text/html;" & chr(34) & " charset=" & chr(34) & "utf-8" & chr(34) & " http-equiv=" & chr(34) & "Content-Type" & chr(34) & "><title>test</title><style type=" & chr(34) & "text/css" & chr(34) & ">" & "</style></head><body>" & str_body &"</body></html>"

jmail.From = mailcomp '寄信人位址
jmail.FromName = "台灣" '寄件人名稱
jmail.ReplyTo = mailcomp '回信位址
jmail.Subject = MailSubject '信件title
jmail.AddRecipient MailTo '收件人地址
jmail.Body = "我們的郵件採用了HTML格式"
JMail.HTMLBody = HTMLMailBody


jmail.MailServerUserName = mailacc ' SMTP 登入帳號
jmail.MailServerPassWord = mailpass ' SMTP 登入密碼
jmail.Send(mailurl) '指定送信伺服器 SMTP
jmail.Close
set jmail = nothing
if Err.Number<>0 then '錯誤處理
'response.write Err.Description 
JMailSend=1
else 
 JMailSend=0
end if 


End Function

2013年9月24日 星期二

JavaScript replace 字數計算,取代空白、換行

前置作業:
請自行下載JQuery檔案以及建置好開發ASP的環境。
重點在於如何使用JavaScript來計算使用者在輸入文字時,如何計算字數。

紅色字為重點

<%@CODEPAGE="65001"%>
<%
'輸出設定成UTF-8'
Response.CodePage = 65001  
Response.CharSet = "utf-8"

'在使用傳統ASP時,為了避免使用者按"上一頁"看到Client的暫存網頁,通常會加上下列這三行'
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文件</title>
<script type="text/javascript" src="js/jquery-2.0.2.min.js"></script>
<script type="text/javascript">
var str_factor=/(\s)|(\n)/g;
$(document).ready(function(){
       //運用JQuery為input id=TB1加入keyup事件
$('#TB1').keyup(function(){
var Word_Number;
var str_temp2 = $('#TB1').val(); //取得input id=TB1的值
if (str_temp2.replace(str_factor, "").length==0) {
$("#SSWl").html(''); //將 div id=SSWl 文字清空
}else{
Word_Number=(str_temp2.replace(str_factor, "")).length;
$("#SSWl").html('目前字數:'+Word_Number);
$('#showmsg').html($('#TB1').val());
}
});
});

function checkform(){
var str_temp = "";
if ($('#TB1').val() == '' || $.trim($('#TB1').val()).length - 1 == -1) {
alert('未填寫');
return false;
} else {
str_temp = $('#TB1').val();
if ((str_temp.replace(str_factor, "")).length < 10) {
alert('字數小於10個字,\n必須至少10個字');
return false;
}else if ((str_temp.replace(str_factor, "")).length > 10){
if ((str_temp.replace(str_factor, "")).length > 50){
 alert('字數最多不能大於50個字');
 return false;
}
}
}
window.document.f1.submit();
}
</script>
</head>
<body>
<%
TB1=Request.Form("TB1")
if NOT ISEmpty(TB1) then
   Response.write "NOT Empty<br />"
   str_temp=replace(TB1,vbcrlf,"" )
   str_temp=replace(str_temp," ","" )
   Response.write "長度="& len(str_temp) & ";" & str_temp '&nbsp;空白符號
else
   Response.write "Empty"
end  if
%>
<div id="SSWl"></div>
<form method="post" name="f1" id="f1" action="test_TextBox.asp" enctype="application/x-www-form-urlencoded">
  <textarea name="TB1" id="TB1" style="width: 450px; height: 180px"></textarea>
  <input type="submit" value="送出" onclick="javascript:checkform();"/>
</form>
<br />
<span id="showmsg">?</span>
</body>
</html>