新增广告操作的核心是处理新增广告表单中的数据,处理后的数据写入广告数据存放文本(data.txt)中。下面来看页面表单的部分,核心的表单代码如下:
广州网站建设
- <div align="left">
- <table width="100%" border="1" cellpadding="3" cellspacing="0"
- bgcolor="#CCCCCC">
- <tr>
- <td width="16%">广告ID:<font color="#FF0000">*</font><br>
- <br>
- </td>
- <td width="84%"><input name="id" type="TEXT" class="input"
- id="id" size="25" maxlength="30"></td>
- </tr>
- <tr>
- <td height="14">广告类型<font color="#FF0000">*</font>
- </td>
- <td height="14"><select name="nx" id="nx">
- <option value="纯HTML">纯HTML</option>
- <option value="脚本式">脚本式</option>
- </select> HTML中的换行符将会被省略掉。</td>
- </tr>
- <tr>
- <td>规格(宽*高)</td>
- <td><input name="gge" type="TEXT" class="input" id="id" value="X"
- size="20" maxlength="30"></td>
- </tr>
- <tr>
- <td height="76" valign="top">
- <p>广告内容<font color="#FF0000">*</font></p>
- </td>
- <td valign="middle"><textarea name="data" cols="80" rows="10"
- wrap="virtual" class="input" id="data" tabindex="2">
- </textarea></td>
- </tr>
- <tr>
- <td height="30">简单注释:</td>
- <td height="30"><textarea name="notice" cols="80" rows="4"
- wrap="virtual" class="input" id="notice" tabindex="2">
- </textarea></td>
- </tr>
- <tr align="center">
- <td height="30" colspan="2"><input type="submit" name="action"
- value="提交" class="botton"> <input type="reset" value="重填"
- class="botton" name="reset"> <input type="button"
- value="返回上一页" onClick="javascript:history.back(-1);"
- class='botton'></td>
- </tr>
- </table>
- </div>
【代码解读】
上述页面模板代码是一个标准的数据表单。其中,表单标签中的值传递给添加广告程序(add.php)处理,添加广告程序将表单传递过来的值,拆分处理成广告存放文件的预定义格式,并写入该文件保存。
添加广告程序(add.php)的代码具体如下:
广州网站建设
- <?php
- /*******************************************
- '文件名:add.php
- '主要功能:添加广告程序
- '说明:
- '*******************************************/
- include_once(dirname(__FILE__)."/inc/include.smarty.php");
- //加载Smarty配置文件
- include(dirname(__FILE__). "/inc/include.function.php"); //加载函数库文件
- require(dirname(__FILE__)."/config/config.php"); //加载容配置文件
- $randrand=rand(0,9999); //生成随机数
- if($_REQUEST['action']=="提交"){
- $id=$_REQUEST[id];
- $nx=$_REQUEST[nx];
- $gge=$_REQUEST[gge];
- $notice=$_REQUEST[notice];
- $data=$_REQUEST[data];
- //处理文件内容
- $msgd=explode("|www_allen|",file_get_contents($dbtable));
- $rows=count($msgd);
- for ($i=0;$i<=$rows;$i++){
- $tmp2=explode("|allen|",$msgd[$i]); //切割字符串
- $msgn[$i]=$tmp2[0];
- if(eregi("^$id$",$msgn[$i])){
- $chj_nr=$msgd[$i];
- }
- }
- if($chj_nr!=""){
- error("该广告ID已经存在,请换一个吧。"); //报错信息
- }
- if($data=="" or $id==""){ //判断数据是否存在
- die("ID或者内容为空!");
- }
- $fp=fopen("$dbtable","a+"); //打开指定文件'a+'(打开文件方式为可读写)
- $all.="$id|allen|$nx|allen|$data|allen|$notice|allen|$gge|www_allen|";
- @fputs($fp,$all) or die(error('写入信息时出错!'));
- if($nx=="纯HTML"){
- $data=allen_thu($data);
- allen_file($data,"$addata/$id.js");
- }else{
- allen_file($data,"$addata/$id.js");
- }
- isok("添加广告成功!请稍等...");
- }
- $smarty->display("./add.html");
- ?>
广州网站设计
- http://localhost/ad/add.php
效果如图15.9所示。添加测试广告数据后单击"提交"按钮,提示提交成功。提示成功页面如图15.10所示。
![]() |
| 图15.9 新增广告 |
![]() |
| 图15.10 添加广告成功页面 |





