+ -
当前位置:首页 → 问答吧 → 自己发明的一种表单自动判断POST地址的方法 ++ 节省文件数

自己发明的一种表单自动判断POST地址的方法 ++ 节省文件数

时间:2009-09-15

来源:互联网

我们在做后台管理某个页面的时候,比如文章编辑页,经常遇到该页面的表单既用来添加、也用来修改,而大多数人的做法是,创建两个页面,或者两个模板,添加文章用addarticle.php,修改文章用editarticle.php,删除文章用delete.php,并且post的地址分别设置为action="addarticle.php" 和"editarticle.php"。。这样子的弊端就是后台文件会变多,一旦有多几个这样的页面,维护修改起来势必也要多花时间
其实我们可以这样写。。总共只有1个页面
[php]
<?php
if (isset($GET['action'])) {
exit;
}
require_once('../include/common.inc.php');
switch ($_GET['action'])
{
case 'new_article':
  $posturl = 'article_edit.php?action=insert';
  break;
case 'insert';
  $title     =  $_POST['title'];
  $cid       =  $_POST['cid'];
  $preview   =  $_POST['preview'];
  $content   =  $_POST['content'];
  $tags      =  $_POST['tags'];
  $date      =  date("Y-m-d H:i:s");  
  $top       =  intval($_POST['top']);
  $etop      =  intval($_POST['editortop']);
  $ftop      =  intval($_POST['flashtop']);
  $top1      =  intval($_POST['top1']);
  $sql = "insert into articles(title,cid,preview,content,tags,date,top,etop,ftop,top1,hits) values('$title','$cid','$preview','$content','$tags','$date','$top','$etop','$ftop','$top1',1)";
  if(mysql_query($sql)){
   $aid = mysql_insert_id();
   //置顶
   if ($top) {
    $sql = "insert into toparticles(aid,title,type) values('$aid','$title',1)";
    mysql_query($sql);   
   }
   //编辑推荐
   if ($etop) {
    $sql = "insert into toparticles(aid,title,type) values('$aid','$title',2)";
    mysql_query($sql);
   }
   //flash幻灯推荐   
   if ($ftop) {
    ;
   }
   turnto("article_list.php","添加文章成功!");
  }else {
   jsjump("back","添加文章失败!");
  }
  exit;
case 'edit_article':
  empty($_GET['id'])?exit():$id = $_GET['id'];
  $posturl = 'article_edit.php?action=update';
  $sql = "select * from articles where id=$id";
  $topsql = "select * from toparticles where aid=$id";
  $rs = mysql_query($sql);
  $article = mysql_fetch_array($rs);
  break;
case 'update':
  $id        =  $_POST['id'];
  $title     =  $_POST['title'];
  $cid       =  $_POST['cid'];
  $preview   =  $_POST['preview'];
  $content   =  $_POST['content'];
  $tags      =  $_POST['tags'];
  $top       =  intval($_POST['top']);
  $etop      =  intval($_POST['editortop']);
  $ftop      =  intval($_POST['flashtop']);
  $top1      =  intval($_POST['top1']);
  $sql = "update articles set title='$title',
    cid='$cid',
    preview='$preview',
    content='$content',
    tags='$tags',
    top='$top',
    etop='$etop',
    ftop='$ftop',
    top1='$top1'
  where id='$id';
  ";
  if(mysql_query($sql)){
   $aid = $id;
   //置顶
   if ($top) {
    $sql = "select * from toparticles where aid=$aid and type=1";
    if (!mysql_fetch_array(mysql_query($sql))) {
     $sql = "insert into toparticles(aid,title,type) values('$aid','$title',1)";
     mysql_query($sql);
         }   
   }else {
    $sql = "select * from toparticles where aid=$aid";
    if (mysql_query($sql)) {
     $sql = "delete from toparticles where aid=$aid and type=1";
     mysql_query($sql);
    }
   }
   //编辑推荐
   if ($etop) {
    $sql = "select id from toparticles where aid=$aid and type=2";
    if (!mysql_fetch_array(mysql_query($sql))) {
     $sql = "insert into toparticles(aid,title,type) values('$aid','$title',2)";
     mysql_query($sql);
         }   
   }else {
    $sql = "select * from toparticles where aid=$aid";
    if (mysql_query($sql)) {
     $sql = "delete from toparticles where aid=$aid and type=2";
     mysql_query($sql);
    }
   }   
   if ($top) {
    ;
   }     
   turnto("article_list.php","修改文章成功!");
  }else {
   jsjump("back","修改文章失败!");
  }
  exit;
case 'delete':
  empty($_GET['id'])?exit():$id = $_GET['id'];
  $sql = "delete from articles where id=$id";
  if(mysql_query($sql)){
   mysql_query("delete from toparticles where aid=$id");
   turnto("article_list.php","删除文章成功!");
  }else {
   turnto("article_list.php","删除文章失败!");
  }
  exit;  
default:
  exit('Error');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body bgcolor="#DDEEFF">
<center><table width="95%" height="auto" cellpadding="0" cellspacing="1" bgcolor="#ADD2DA">
  <tr>
    <td height="190" valign="top"><br/>
    <form enctype="multipart/form-data" id="form1" action="<?= $posturl?>" method="POST">
    <input type="hidden" name="id" id="id" value="<?= $article['id']?>"/>
      <table width="98%" align="center" cellpadding="0" cellspacing="1" style="word-break:break-all;">
      <tr align="center">
        <td height="15" align="center"bgcolor="#F1F1F1">标题</td>
        <td height="20" align="left" bgcolor="#FFFFFF"><input type="text" name="title" value="<?=$article['title']?>" style="width:500px;"/></td>
      </tr>      
      <tr align="center">
        <td width="80" height="14" align="center"bgcolor="#F1F1F1">文章分类</td>
        <td height="20" align="left" bgcolor="#FFFFFF"><span class="STYLE1">
          <select name="cid" id="cid">
           <?php foreach ($categories as $category) {?>
            <option value="<?= $category['id']?>"><?= $category['name']?></option>
            <?php ;}?>
          </select>
        </span></td>
        </tr>
      <tr>
        <td height="183" align="center" bgcolor="#F1F1F1"><span class="menuCss1">文章预览</span></td>
        <td height="234" align="left" bgcolor="#FFFFFF">
        <?php
     include_once("./editor/fck/fckeditor.php");
  $oFCKeditor = new FCKeditor('preview') ;
  $oFCKeditor->BasePath = "./editor/fck/";
  $oFCKeditor->Value  =  $article['preview'] ;
  $oFCKeditor->Height="100%";
  $oFCKeditor->Width="100%";
  $oFCKeditor->Create() ;
  ?>
        </td>
      </tr>
      <tr>
        <td height="183" align="center" bgcolor="#F1F1F1"><span class="menuCss1">文章正文</span></td>
        <td height="400" align="left" bgcolor="#FFFFFF">
        <?php
  $oFCKeditor = new FCKeditor('content') ;
  $oFCKeditor->BasePath = "./editor/fck/";
  $oFCKeditor->Value  =  $article['content'] ;
  $oFCKeditor->Height="100%";
  $oFCKeditor->Width="100%";
  $oFCKeditor->Create() ;
  ?>        
        </td>
      </tr>
      <tr align="center">
        <td height="15" align="center"bgcolor="#F1F1F1">关键词</td>
        <td height="20" align="left" bgcolor="#FFFFFF"><input type="text" name="tags" value="<?=$article['tags']?>" style="width:400px;"/></td>
      </tr>      
      <tr>
        <td height="20" align="center" bgcolor="#F1F1F1">推荐</td>
        <td height="20" align="left" bgcolor="#FFFFFF">
        <input type="checkbox" id="top" name="top" value="1" <?=$article['top']?'checked':''?>>置顶推荐</input>
        <input type="checkbox" id="editortop" name="editortop" value="1"<?=$article['etop']?'checked':''?> >编辑推荐</input>
        <input type="checkbox" id="flashtop" name="flashtop" value="1" <?=$article['ftop']?'checked':''?>>幻灯推荐</input>
        <input type="checkbox" id="top1" name="top1" value="1" <?=$article['top1']?'checked':''?>>头条推荐</input>
        </td>
      </tr>
      <tr>
        <td height="30" align="center" bgcolor="#F1F1F1">附件</td>
        <td height="30" align="left" bgcolor="#FFFFFF"><input type="file" name="file" id="file"/></td>
      </tr>      
      <tr>
        <td height="30" align="center" bgcolor="#F1F1F1">配图</td>
        <td height="30" align="left" bgcolor="#FFFFFF"><input type="file" name="showpic" id="showpic"/></td>
      </tr>
      <tr>
        <td height="40" colspan="2" align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="submit"/></td>
      </tr>   
    </table></form></td>
  </tr>
  <tr height="10" align="center"></tr>
</table></center>
</body>
</html>
[/php]

作者: oik550   发布时间: 2009-09-15

if (isset($GET['action'])) {

exit;

}
啥意思

作者: yafeikf   发布时间: 2009-09-15

Wow, It's fucking excellent...........................................................................................................................................................

作者: c10h15n   发布时间: 2009-09-15

楼主很强!

作者: wade2006   发布时间: 2009-09-15

楼主了解下单入口模式

作者: ZendFramework   发布时间: 2009-09-15

这个也叫自己发明!
那只能够说明你都没有看过开源程序!

作者: 齐迹   发布时间: 2009-09-15

lz,要多学习。“发明”这个词要谨慎使用。

作者: flyeast   发布时间: 2009-09-15

值得鼓励啊,新人发个东西不容易,有点想法分享给大家更不容易(即使这方法对于某些老鸟已经过时了)。
支持楼主!

作者: timeshi   发布时间: 2009-09-15

:funk:很伟大的发明!

作者: yilong   发布时间: 2009-09-15

额。。。单一入口。。。解决起来更轻松- -还验证啥表单地址。。
不过鼓励你的这种行为。多思考,才有进步。
但是。。。。重复造轮子是吃力不讨好的

作者: 某个人   发布时间: 2009-09-15

晕,发明~楼主闭门造车,看看现在开源的PHP框架吧。

作者: sinopf   发布时间: 2009-09-15

某个人
了解了。。。框架都是单入口。这个确实有用
版主闭帖吧

作者: oik550   发布时间: 2009-09-16

确实,重复造轮子了。开源的意义之一就是在于你可以广泛的吸取别人的思想,而非代码。
当然,楼主的创新精神值得鼓励。

作者: lvbibo   发布时间: 2009-09-16

强大的发明。

作者: myBe   发布时间: 2009-09-16

呵呵。后生可畏啊

作者: jxncpbs   发布时间: 2009-09-16

{:2_154:}

楼主的头像很强大~~~

作者: linzhoulxyz   发布时间: 2009-09-16



这个不能叫发明吧
这个是基础,人人都知道的啊
不过我倒是有真的发明,呵呵
暂时不告诉你

作者: lqfox66   发布时间: 2010-09-14

我有更自动的方法呵呵

作者: lqfox66   发布时间: 2010-09-14



index.php

  1. <?php
  2. class artitle extends app
  3. {
  4.      function indexAction()
  5.      {
  6.            $this->tpl->dispaly('index.html');
  7.       }
  8.       function addAction()
  9.      {
  10.           //添加文章的代码
  11.          
  12.       }
  13. }
  14. app::run(__FILE__);
  15. ?>
复制代码

模板文件 index.html

  1. <form action='?action=add'>
  2. ......表单项目
  3. <input type='submit'/>
  4. </form>
复制代码

作者: lqfox66   发布时间: 2010-09-14

相关阅读 更多