一个完整的PHP表单提交开发实例(提交订单入库并同时发送邮件到指定EMAIL地址)
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C.[insert.php]
<?php $con = mysql_connect("localhost","root","");//连接数据库 echo "$stm" ; if (!$con)
{ die('Could not connect: ' . mysql_error()); } mysql_select_db("ymtest", $con); mysql_query('set names GB2312');//设置插入数据库的字符集格式,避免数据库中的数据出现 乱码 $sql="insert into `ymtest`.`order_sd` (name,product,ordNum,tell,email,address,ordTime) values ('$name','$product','$ordNum','$tell','$email','$address','$time')"; //数据入库 if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?>
/* Private Variables */ var $sock;
/* Constractor */ function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) { $this->debug = FALSE; $this->smtp_port = $smtp_port; $this->relay_host = $relay_host; $this->time_out = 30; //is used in fsockopen() $this->auth = $auth;//auth $this->user = $user; $this->pass = $pass; $this->host_name = "localhost"; //is used in HELO command $this->log_file = ""; $this->sock = FALSE; }
<tr align=center bgcolor=5272BA> <td colspan=2 height=30>客户[".$name."]的订单信息!</td>
</tr> <tr bgcolor=5272BA>
<td align=right width='200'>联系人:</td> <td>".$name."</td> </tr> <tr bgcolor=5272BA> <td align=right width='200'>订购产品:</td> <td>".$product."</td> </tr> <tr bgcolor=5272BA> <td align=right width='200'>订购数量:</td> <td>".$ordNum."套</td> </tr> <tr bgcolor=5272BA> <td align=right>电话:</td> <td>".$tell."</td> </tr> <tr bgcolor=5272BA> <td align=right>Email:</td> <td>".$email."</td>
<tr bgcolor=5272BA> <td></td> <td><input type="submit" class="main_btn " name="Submit" value="提交"></td>
</tr>
</table>
</form> </body> </html>
下面是页面效果
B.[order.php]
/* Main Function */ function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") { $mail_from = $this->get_address($this->strip_comment($from)); $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body); $header .= "MIME-Version:1.0\r\n"; if($mailtype=="HTML") { $header .= "Content-Type:text/html\r\n"; }
一个完整的 PHP+MySQL 开发实例 ——(提交订单入库并同时发送邮件到指定 Email 地址)
本例共包含以下文档:index.html (订单提交首页),order.php(接受表单数据,发送 Email 邮件,打印表单),insert.php(提交表单数据入库),smtp.php(SMTP 邮件发送类) 首先,设计数据库,规划数据表和字段
提交的表单数据被插入数据库ห้องสมุดไป่ตู้
D.[smtp.php]
<?php class smtp { /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $user; var $pass;
</tr> <tr bgcolor=5272BA>
<td align=right>地址:</td> <td>".$address."</td> </tr>
<tr bgcolor=5272BA> <td align=right></td> <td align=right>下单时间:".$time."</td>
以下为实现代码(CSS 什么的就不说了):
A.[index.html]
<html> <head> <title></title> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'> <style> .title { font-family: '宋体'; font-size: 13px; line-height: 150% ; color: #FFFFFF} </style> </head> <form id="form1" name="form1" method="post" action="order.php"> <body bgcolor=dfdfdf text=#000000> <table width=80% border=0 cellspacing=1 cellpadding=2 class=title bgcolor=#33C align=center>
<tr align=center bgcolor=5272BA> <td colspan=2 height=30>订单提交</td>
</tr> <tr bgcolor=5272BA>
<td align=right width='200'>订购产品:</td> <td><input name="product" type="radio" class="mt5" checked="checked" value="测试产品 ">测试产品</td> </tr> <tr bgcolor=5272BA> <td align=right width='200'>订购数量:</td> <td><input type="text" class="main_input" name="ordNum" value="1" />套</td> </tr>
</tr>
</table> </body> </html>"; require("insert.php"); //提交数据到数据库 require("smtp.php"); //SMTP 类 发送订单到 Email ########################################## $smtpserver = "";//SMTP 服务器 $smtpserverport = 25;//SMTP 服务器端口 $smtpusermail = "xxxxx@";//SMTP 服务器的用户邮箱 $smtpemailto = "xxxxxxx@";//发送给谁 $smtpuser = "xxxxxxxx@";//SMTP 服务器的用户帐号 $smtppass = "xxxxxxxx";//SMTP 服务器的用户密码 $mailsubject = "订单 ";//邮件主题 $mailbody = $stm;//邮件内容 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT 为文本邮件 ########################################## $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个 true 是表示使用身份验证,否则不使用身份验证。 $smtp->debug = TRUE;//是否显示发送的调试信息 $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); exit; ?> 下面是执行效果
<?php date_default_timezone_set('Asia/Shanghai');//时区设置
//接收表单提交到的数据,生命变量 $name = $_POST['name']; $product = $_POST['product']; $ordNum = $_POST['ordNum']; $tell = $_POST['tell']; $address = $_POST['address']; $email = $_POST['email']; $time = date("Y-m-d H:i:s"); $stm="<html> <head> <title></title> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'> <style> .title { font-family: '宋体'; font-size: 13px; line-height: 150% ; color: #FFFFFF} </style> </head> <body bgcolor=dfdfdf text=#000000> <table width=80% border=0 cellspacing=1 cellpadding=2 class=title bgcolor=#33C align=center>
<tr bgcolor=5272BA> <td align=right width='200'>姓名:</td> <td><input type="text" class="main_input" name="name" autocomplete="on" /></td> </tr> <tr bgcolor=5272BA> <td align=right>电话:</td> <td><input type="text" class="main_input" name="tell" maxlength="11" autocomplete="on" /></td> </tr> <tr bgcolor=5272BA> <td align=right>Email:</td> <td><input type="text" class="main_input" name="email" maxlength="30" autocomplete="on" /></td> </tr> <tr bgcolor=5272BA> <td align=right>地址:</td> <td><input type="text" class="main_input" name="address" maxlength="50" autocomplete="on" /></td> </tr>
<?php $con = mysql_connect("localhost","root","");//连接数据库 echo "$stm" ; if (!$con)
{ die('Could not connect: ' . mysql_error()); } mysql_select_db("ymtest", $con); mysql_query('set names GB2312');//设置插入数据库的字符集格式,避免数据库中的数据出现 乱码 $sql="insert into `ymtest`.`order_sd` (name,product,ordNum,tell,email,address,ordTime) values ('$name','$product','$ordNum','$tell','$email','$address','$time')"; //数据入库 if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?>
/* Private Variables */ var $sock;
/* Constractor */ function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) { $this->debug = FALSE; $this->smtp_port = $smtp_port; $this->relay_host = $relay_host; $this->time_out = 30; //is used in fsockopen() $this->auth = $auth;//auth $this->user = $user; $this->pass = $pass; $this->host_name = "localhost"; //is used in HELO command $this->log_file = ""; $this->sock = FALSE; }
<tr align=center bgcolor=5272BA> <td colspan=2 height=30>客户[".$name."]的订单信息!</td>
</tr> <tr bgcolor=5272BA>
<td align=right width='200'>联系人:</td> <td>".$name."</td> </tr> <tr bgcolor=5272BA> <td align=right width='200'>订购产品:</td> <td>".$product."</td> </tr> <tr bgcolor=5272BA> <td align=right width='200'>订购数量:</td> <td>".$ordNum."套</td> </tr> <tr bgcolor=5272BA> <td align=right>电话:</td> <td>".$tell."</td> </tr> <tr bgcolor=5272BA> <td align=right>Email:</td> <td>".$email."</td>
<tr bgcolor=5272BA> <td></td> <td><input type="submit" class="main_btn " name="Submit" value="提交"></td>
</tr>
</table>
</form> </body> </html>
下面是页面效果
B.[order.php]
/* Main Function */ function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") { $mail_from = $this->get_address($this->strip_comment($from)); $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body); $header .= "MIME-Version:1.0\r\n"; if($mailtype=="HTML") { $header .= "Content-Type:text/html\r\n"; }
一个完整的 PHP+MySQL 开发实例 ——(提交订单入库并同时发送邮件到指定 Email 地址)
本例共包含以下文档:index.html (订单提交首页),order.php(接受表单数据,发送 Email 邮件,打印表单),insert.php(提交表单数据入库),smtp.php(SMTP 邮件发送类) 首先,设计数据库,规划数据表和字段
提交的表单数据被插入数据库ห้องสมุดไป่ตู้
D.[smtp.php]
<?php class smtp { /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $user; var $pass;
</tr> <tr bgcolor=5272BA>
<td align=right>地址:</td> <td>".$address."</td> </tr>
<tr bgcolor=5272BA> <td align=right></td> <td align=right>下单时间:".$time."</td>
以下为实现代码(CSS 什么的就不说了):
A.[index.html]
<html> <head> <title></title> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'> <style> .title { font-family: '宋体'; font-size: 13px; line-height: 150% ; color: #FFFFFF} </style> </head> <form id="form1" name="form1" method="post" action="order.php"> <body bgcolor=dfdfdf text=#000000> <table width=80% border=0 cellspacing=1 cellpadding=2 class=title bgcolor=#33C align=center>
<tr align=center bgcolor=5272BA> <td colspan=2 height=30>订单提交</td>
</tr> <tr bgcolor=5272BA>
<td align=right width='200'>订购产品:</td> <td><input name="product" type="radio" class="mt5" checked="checked" value="测试产品 ">测试产品</td> </tr> <tr bgcolor=5272BA> <td align=right width='200'>订购数量:</td> <td><input type="text" class="main_input" name="ordNum" value="1" />套</td> </tr>
</tr>
</table> </body> </html>"; require("insert.php"); //提交数据到数据库 require("smtp.php"); //SMTP 类 发送订单到 Email ########################################## $smtpserver = "";//SMTP 服务器 $smtpserverport = 25;//SMTP 服务器端口 $smtpusermail = "xxxxx@";//SMTP 服务器的用户邮箱 $smtpemailto = "xxxxxxx@";//发送给谁 $smtpuser = "xxxxxxxx@";//SMTP 服务器的用户帐号 $smtppass = "xxxxxxxx";//SMTP 服务器的用户密码 $mailsubject = "订单 ";//邮件主题 $mailbody = $stm;//邮件内容 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT 为文本邮件 ########################################## $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个 true 是表示使用身份验证,否则不使用身份验证。 $smtp->debug = TRUE;//是否显示发送的调试信息 $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); exit; ?> 下面是执行效果
<?php date_default_timezone_set('Asia/Shanghai');//时区设置
//接收表单提交到的数据,生命变量 $name = $_POST['name']; $product = $_POST['product']; $ordNum = $_POST['ordNum']; $tell = $_POST['tell']; $address = $_POST['address']; $email = $_POST['email']; $time = date("Y-m-d H:i:s"); $stm="<html> <head> <title></title> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'> <style> .title { font-family: '宋体'; font-size: 13px; line-height: 150% ; color: #FFFFFF} </style> </head> <body bgcolor=dfdfdf text=#000000> <table width=80% border=0 cellspacing=1 cellpadding=2 class=title bgcolor=#33C align=center>
<tr bgcolor=5272BA> <td align=right width='200'>姓名:</td> <td><input type="text" class="main_input" name="name" autocomplete="on" /></td> </tr> <tr bgcolor=5272BA> <td align=right>电话:</td> <td><input type="text" class="main_input" name="tell" maxlength="11" autocomplete="on" /></td> </tr> <tr bgcolor=5272BA> <td align=right>Email:</td> <td><input type="text" class="main_input" name="email" maxlength="30" autocomplete="on" /></td> </tr> <tr bgcolor=5272BA> <td align=right>地址:</td> <td><input type="text" class="main_input" name="address" maxlength="50" autocomplete="on" /></td> </tr>