主從式架構
一個好的主從式的應用程式, 應該在使用者輸入時便立即確認其輸入值是否為 valid (可用 VBScript, JScript, JavaScript, PerlScript 等.), 然後才將 確認過的資料後傳至伺服器端處理程式 (如 CGI, ASP 等)。在以下範例中,使用者 可以在郵遞區號內輸入“aaa“,然後按”確定“,網頁會告訴你輸入的郵遞區號必須是數字。
範例:
form 原始碼:
<form name=form1 action="vb6.pl"> 郵遞區號: <input type=text name=zip size=5 onBlur="validate(this.form)"> 傳真號碼: <input type=text name=fax size=15> <input type=submit value="確定"> </form>
javascript 原始碼:
<script language="javascript">
function validate(tform1)
{
// 確保輸入的資料長度為三
if(tform1.zip.value.length != 3)
{
alert("Please enter a numeric, 3-digit code.");
// 既然錯了,強迫再回去輸入
tform1.zip.focus();
}
else
{
// 確保輸入的是數字
if(isNaN(tform1.zip.value))
{
alert("Please enter a numeric, 3-digit code.");
tform1.zip.focus();
}
else
{
// 確保郵遞區號介於 100 與 999 之間
zip_num = parseInt(tform1.zip.value);
if (zip_num > 999 || zip_num < 100 )
{
alert("Please enter a numeric, 3-digit code.");
tform1.zip.focus();
}
}
}
}
</script>
Perl 原始碼:
#!/usr/local/bin/perl
require 'forms-lib.pl';
print "content-type: text/html\n\n";
%input = &GetFormInput();
$zip = $input{'zip'} || "402";
$fax = $input{'fax'} || "(04) 3742337";
print <<EndofMessage;
<html>
<head><title>A Sample Client-Server Application</title></head>
<body>
<h3 align=center>A Sample Client-Server Application</h3>
In this simple application, we use Javascript as the front-end
application which is used to validate inputs, and then submit
it to the server using CGI. The CGI program is written in Perl.
<hr>
<center>
<table border=1>
<tr>
<th>Zip Code</th><th>Fax Number</th>
</tr>
<tr>
<td>$zip</td><td>$fax</td>
</tr>
</table>
</center>
</body>
</html>
EndofMessage
沒有留言:
張貼留言