麻辣家族討論版版's Archiver

小誌 發表於 2011-4-24 14:55

13-1-1 程式寫作說明

[color=DarkRed][size=4][b]13-1-1   程式寫作說明[/b][/size][/color]在index.htm賀卡寄送網頁的部分有兩個重點:

1.表單的動作對象,也就是「Action」屬性值的指定:[code]<form method=POST action=ecard.php>[/code]以本例來說,使用者在index.htm頁面中填寫好相關資料與選擇賀卡後,按下「寄出賀卡」按鈕後,表單資料要送交『ecard.php』程式做處理,所以「Action」屬性值指定為賀卡處理程式『ecard.php』。


2.賀卡圖片的設定,index.htm頁面中所見的賀卡小圖片都是讓寄件者參考選擇用,真正決定要寄出哪張賀卡圖片則是在於賀卡小圖片上方的選項按鈕(單選按鈕 Radio),這些選項按鈕的名稱在本例中都叫做「card」,但是各個選項按鈕的Value屬性值卻不同,其值分別是各賀卡圖片的檔案名稱:[code]        <td>
                <input type=radio value=2.jpg name=card checked>
                <img border=0 src=2.jpg width=180 height=133 align=left>
        </td>
        <td>
                <input type=radio value=9.jpg name=card>
                <img border=0 src=9.jpg width=180 height=134 align=left>
        </td>
        <td>
                <input type=radio value=16.jpg name=card >
                <img border=0 src=16.jpg width=180 height=134 align=left>
</td>[/code]

小誌 發表於 2011-4-24 14:56

當index.htm中的表單資料送交『ecard.php』程式時,先進行郵件Email的正確性簡易驗證,在本例中只檢驗Email中是否含有「@」符號,若無,則認定其Email不正確,Email不正確就不進行賀卡郵件的寄送程序,此時會出現一個「Sorry!! Email郵件信箱填寫有誤喔!!」的JavaScript訊息視窗:
[attach]5819[/attach]
圖13-4  Email錯誤出現提示視窗。

按下JavaScript訊息視窗中的「確定」按鈕後,即可回到上ㄧ網頁(index.htm)畫面修正輸入的資料。[code]//簡易驗證收件與寄件信箱是否正確
If ((!(strpos($fromEmail,'@'))) || (!(strpos($toEmail,'@'))))
{
//Email不正確出現一個錯誤提示的JavaScript訊息視窗
      echo "<script>\n";
      echo "alert('Sorry!!Email郵件信箱填寫有誤喔!!');\n";
      echo "window.history.go(-1);\n";
      echo "</script>";
}[/code]若收件與寄件者的Email信箱通過驗證,則開始郵件的寄出程序,在本例中是使用CDONTS.NewMail 物件來寄送賀卡郵件,所以賀卡圖片以夾帶附件的方式來隨信件寄出,CONDTS.NewMail物件的『AttachFile』方法:[code]Mymail.AttachFile(Source [,FileName] [,EncodingMethod])[/code]

小誌 發表於 2011-4-24 14:57

以下為完整的程式與網頁列表:
Index.htm[code]<html>

<head>
<title>簡易電子賀卡-以夾帶附件檔案的方式寄出賀卡</title>
</head>

<body background=bg.gif>

<p><font color=#800000 size=4><b>簡易電子賀卡</b></font>-以夾帶附件檔案的方式寄出賀卡</p>
<hr>
<form method=POST action=ecard.php>
<div align=center>
<table border=0 id=table1 style=font-size: 10pt>
<tr>
<td>寄件人:</td>
<td><input type=text name=fromname size=20></td>
<td>寄件信箱:</td>
<td><input type=text name=fromemail size=40></td>
</tr>
<tr>
<td>收件人:</td>
<td><input type=text name=toname size=20></td>
<td>收件信箱:</td>
<td><input type=text name=toemail size=40></td>
</tr>
<tr>
<td>訊息:</td>
<td colspan=3><textarea rows=6 name=message cols=71></textarea></td>
</tr>
<tr>
<td>賀卡:</td>
<td colspan=3> <table border=0 width=100% id=table2>
<tr>
<td>
<input type=radio value=2.jpg name=card checked>
<img border=0 src=2.jpg width=180 height=133 align=left>
</td>
<td>
<input type=radio value=9.jpg name=card>
<img border=0 src=9.jpg width=180 height=134 align=left>
</td>
<td>
<input type=radio name=card value=16.jpg>
<img border=0 src=16.jpg width=180 height=134 align=left>
</td>
</tr>
<tr>
<td>
<input type=radio name=card value=11.gif>
<img border=0 src=11.gif width=180 height=136 align=left>
</td>
<td>
<input type=radio name=card value=12.jpg>
<img border=0 src=12.jpg width=180 height=134 align=left>
</td>
<td>
<input type=radio name=crad value=20.jpg>
<img border=0 src=20.jpg width=180 height=134 align=left>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan=4><input type=submit value=送出賀卡 name=B1></td>
</tr>
</table>
</div>
</form>

</body>

</html>[/code]

小誌 發表於 2011-4-24 14:57

ecard.php[code]<?
//接收來自表單的欄位資訊
$fromName=$_REQUEST["fromName"];
$fromEmail=$_REQUEST["fromEmail"];
$toName=$_REQUEST["toName"];
$toEmail=$_REQUEST["toEmail"];
$Message=$_REQUEST["message"];
$Card=$_REQUEST["card"];

//簡易驗證收件與寄件信箱是否正確
If ((!(strpos($fromEmail,'@'))) || (!(strpos($toEmail,'@'))))
{
//Email不正確出現一個錯誤提示的JavaScript訊息視窗
      echo "<script>\n";
      echo "alert('Sorry!!Email郵件信箱填寫有誤喔!!');\n";
      echo "window.history.go(-1);\n";
      echo "</script>";
}
Else
{
       //建立郵寄物件
       $Mail = new COM("CDONTS.NewMail");
                $Mail->To=$toName . "<" . $toEmail . ">";
                $Mail->From=$fromName . "<" . $fromEmail . ">";
                $Mail->Subject="來自" . $fromName . "的祝福卡片";
                $Mail->Body=$Message;
                //夾帶賀卡圖檔
                $Mail->AttachFile($Card);
                $Mail->Send();
       
        //告知賀卡已經寄出,返回賀卡選擇頁面       
      echo "<script>\n";
      echo "alert('恭喜你!!賀卡已經順利寄出!!');\n";
      echo "window.location='index.htm';\n";
      echo "</script>";
}               
?>[/code]

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供