首先编写html页面:
<meta charset="UTF-8"> <title>订单页面</title><h2>Jason的购物清单</h2>
再用PHP编写服务器端脚本文件processorder.php
<?php $cloths=$_POST['cloths']; $shoes=$_POST['shoes']; $glasses=$_POST['glasses']; $address=$_POST['address']; $DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT']; //设置时区 date_default_timezone_set('Asia/Shanghai'); //按指定格式输出日期 $date=date('Y-m-d H:i');?> <meta charset="UTF-8"> <title>订单结果</title><h2>Jason的购物车</h2><h3>订单结果</h3><?php echo '<p>订单提交中时间:'.$date.''; echo '<p>您的具体购物清单是:</p>'; //获取商品总数量 $total_qty=0; $total_qty=$cloths+$shoes+$glasses; echo '商品总数量:'.$total_qty.'<br>'; if($total_qty==0){ echo '您没有购买任何商品!'; }else{ if($cloths>0){ echo $cloths.'件男装<br>'; } if($shoes>0){ echo $shoes.'双鞋子<br>'; } if($glasses>0){ echo $glasses.'副眼镜<br>'; } } //获取商品总价 $total_amount=0.00; const CLOTHS_PRICE=100; const SHOES_PRICE=300; const GLASSES_PRICE=28; $total_amount=$cloths*CLOTHS_PRICE+$shoes*SHOES_PRICE+$glasses*GLASSES_PRICE; $total_amount=number_format($total_amount,2,'.',' '); echo '<p>商品总价:¥'.$total_amount.'</p>'; echo '<p>收货地址:'.$address.'</p>'; //设置文件输出内容和格式 $out_put_string=$date."\t".$cloths."件男装\t".$shoes."双鞋子\t".$glasses."副眼镜\t\总价:¥".$total_amount." 收货地址:\t".$address."\n"; //打开文件,(追加模式+二进制模式) @$fp=fopen("$DOCUMENT_ROOT/L02/files/orders.text",'ab'); flock($fp,LOCK_EX); if(!$fp){ echo "<p><strong>您的订单没有提交完成,请再试一次。</strong></p>"; exit; } //将数据写入到文件 fwrite($fp,$out_put_string,strlen($out_put_string)); flock($fp,LOCK_UN); //关闭文件流 fclose($fp); echo "<p>数据保存完成</p>";?>