html和PHP分开写,你以为是.Net啊

解决方案 »

  1.   

    不可能完全分开的,采用MVC模型效果会好点
      

  2.   

    给个登录验证的例子吧:login.htm
    -----------------------------------------------------
    <table width="160" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#dddddd" bgcolor="#FFFFFF">
    <form action="action.php" method="post" enctype="multipart/form-data" name="form1">
      <tr> 
        <td height="24" background="images/title.jpg"> 用户登录</td>
      </tr>
      <tr> 
        <td> <table width="100%" border="0" cellspacing="0" cellpadding="2">
            <tr> 
              <td align="right">帐号: </td>
              <td><input name="logname" type="text" class="box" id="logname" size="13" maxlength="25"> 
              </td>
            </tr>
            <tr> 
              <td align="right">密码:</td>
              <td><input name="logpsd" type="password" class="box" id="logpsd"" size="13" maxlength="25"></td>
            </tr>
          </table></td>
      </tr>
      <tr> 
        <td height="30" align="center">
    <input name="act" type="hidden" id="act" value="login">
    <input name="send" type="submit" id="send" value="用户登录" class="btn">
    </td>
      </tr>
     </form>
    </table>login.php
    用于登录界面的显示
    -----------------------------------------------------
    require_once "global.php";
    session_start();
    require_once "class/view.class.php";
    $v = new view($path,$hostname, $username, $password, $db_name, $pconnect);
    $v->show_login();view.class.php
    ------------------------------------------------------
    /**  BEGIN class
    *
    *   类名:view
    *   功能:页面视图,本类是由phplib的模板类派生,我只保留了主要注释,构造函数中进行了数据库的初始化连接及模板的初始化。
    *   时间:2003.7.5
    */
    require_once "template.class.php";
    class view extends Template
    {
        var $db;
        var $t;
        /**  BEGIN function
        *
        *   功能:构造
        *
        */
        function view($path,$hostname, $username, $password, $db_name, $pconnect)
        {
            $this->db = new db($hostname, $username, $password, $db_name, $pconnect);
            $this->t = new Template($path);
        }    /**  BEGIN function
        *
        *   功能:设置头尾
        *
        */
        function set_header_footer()
        {
            $this->t->set_file("header","header.htm");
            $this->t->set_file("main","main.htm");
            $this->t->set_file("footer","footer.htm");
        }    /**  BEGIN function
        *
        *   功能:解析头尾
        *
        */
        function parse_header_footer()
        {
            $this->t->parse("header","header");
            $this->t->parse("main","main");
            $this->t->parse("footer","footer");
        }    /**  
        *   功能:显示登录界面
        *
        */
        function show_login()
        {
            $this->t->set_file("login","login.htm");
            $this->t->parse("out","login");
            $this->output("out");
        }    /**  BEGIN function
        *
        *   功能:页面输出
        */
        function output($handle)
        {
            $this->t->p($handle);
        }
    }action.php
    用于控制处理程序,
    --------------------------------------------------
    require_once "global.php";
    session_start();
    require_once "class/action.class.php";
    $a = new action($hostname, $username, $password, $db_name, $pconnect);
    /* 登录 */
    if(isset($_POST['act'])&&$_POST['act']=='login')
    {
        $a->dologin($_POST['logname'],$_POST['logpsd']);
    }action.class.php
    用于完成处理。
    ---------------------------------------------------
    /**  BEGIN class 
    *
    *   类名:action
    *   功能:处理
    *   时间:2003.7.5
    */
    require_once "check.class.php";
    class action extends check
    {
        var $db;
        var $c;
        /**  BEGIN function 
        *
        *   功能:构造
        *       
        */
        function action($hostname, $username, $password, $db_name, $pconnect)
        {
            $this->db = new db($hostname, $username, $password, $db_name, $pconnect);
            $this->c = new check;
        }
        
        /**  BEGIN function 
        *
        *   功能:处理登录
        *       
        */
        function dologin($logname,$logpsd)
        {
            $msg = '';
            if($logname=='')
            {
                alert_notes("你没有输入用户名!"); 
            }
            else
            {
                $msg.= $this->c->checklogname($logname,"用户名");
            }
            if(!$logpsd)
            {
                alert_notes("你没有输入密码!");
            }
            else
            {
                $msg.= $this->c->checkpass($logpsd,"用户");
            }
            if($msg!='')
            {
                alert_notes($msg);
            }
            else
            {
                $password = md5($logpsd);
                $sql_login = "select * from user where u_name='".$logname."' and u_psd='".$password."'";
                $res_login = $this->db->query($sql_login);
                $num_login = $this->db->num_rows($res_login);
                if($num_login==1)
                {
                    alert("登录成功!");
                    $arr_login = $this->db->fetch_array($res_login);
                    $_SESSION['u_name'] = $arr_login['u_name'];
                    $_SESSION['u_code'] = $arr_login['u_code'];                /* 查找权限 */
                    $sql = "select * from auth where a_id='".$arr_login['a_id']."'";
                    $res = $this->db->query($sql);
                    $arr = $this->db->fetch_array($res);
                    $_SESSION['a_type']  = $arr['a_type'];
                    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=admin.php\">";
                }
                else
                {
                    alert_notes("登录失败!");
                }
            }
        }
    }以上只给出了主要文件,global.php是一个文件引入文件,主要包含了数据库边接的建立,函数的引入,我就不给出了,否则放不下了。
    在如上所示的处理中,html中是没有一行php代码的,至少到现在为止我的html中仍然还未出现非要用php代码的情况。
      

  3.   

    模板是其中之一
    <?php
      include("./template.inc");  # create Template instance called $t  
      $t = new Template(".", "keep");  # define variables named page and box, referencing files
      $t->set_file(array(
         "page" => "page.htm",
         "box" => "box.htm",
         "box_1"  => "box.htm"));  # extract the block named "row" from "box", creating a
      # reference to {rows} in "box".
      $t->set_block("box", "row", "rows");
      $t->set_block("box_1", "row", "rows");  # define the variables TITLE and PAGETITLE
      $t->set_var(array("TITLE"     => "Testpage",
                        "PAGETITLE" => "hugo"));  # define NUM and BIGNUM, then append "row" to "rows"...
      for ($i=1; $i<=3; $i++) {
        $n  = $i;
        $nn = $i*10;
        $t->set_var(array("NUM" => $n, "BIGNUM" => $nn));
        $t->parse("rows", "row", true);
      }  # build out from box, then build out from page...
      $t->parse("OUT", array("box", "page"));  # finish out and print it.
      $t->p("OUT");
    ?>page.htm
    <html>
     <head><title>{PAGETITLE}</title></head>
     <body bgcolor="#ffffff">
     <table border=1 cellpadding=4 cellspacing=0 bgcolor="#eeeeee">
      <tr>
       <td colspan=2><h1>{PAGETITLE}</h1></td>
      </tr>
      <tr>
       <td>{OUT}</td>
       <td>Content</td>
      </tr>
     </table>
     </body>
    </html>box.htm
    <!-- start box.ihtml -->
    <table border=1 bgcolor="#cccccc" cellpadding=4 cellspacing=0>
     <tr>
      <td colspan=2><b>{TITLE}</b></td>
     </tr>
      <!-- BEGIN row -->
      <tr>
       <td>{NUM}</td>
       <td>{BIGNUM}
      </tr>
      <!-- END row -->
    </table>
    <!-- end box.ihtml -->
      

  4.   

    上面少了一个check.class.php,那是一个数据检查类,action.class.php是由他派生的,主要是为了用到里面的数据检查方法,太长了,不帖了。
      

  5.   

    <!--templete.htm-->
    <html>
    <head>
    <title>{title}</title>
    </head>
    <body>
    <center>{body}</script>
    </body>
    </html><!--template-->
    <?php
        include "template.inc";
        $tpl=new Template("./");
        $tpl->set_file("template","template.htm");
        $tpl->set_var("title","模板测试");
        $tpl->set_var("body","PHP和HTML分离");
        $tpl->parse("output","template");
        $tpl->p("output");
    ?>在上面提到的template.inc文件,你可以去网上找一下PHPlib,里面有这个类,类似的类还有smarty等。
      

  6.   

    你是想功能用php寫,網頁用HTML寫吧
    其實也很簡單test.html
    <html>
    <form action="test.php" method="post">
      <input name="t1">
      <input type="submit" value="go">
    </form>
    </html>test.php
    <?php
      if ( !empty($HTTP_GET_VARS["t1"]) || !empty($HTTP_POST_VARS["t1"]) )
      {
         print $HTTP_POST_VARS["t1"];
      } else {
        print "none";
      }
    ?>
      

  7.   

    用模板写程序,基本上可以把html和php分开,
      

  8.   

    你看看这个文档吧!
    文档附带有程序代码!
    http://www.csdn.net/develop/Read_Article.asp?Id=21639
    http://www.csdn.net/develop/Read_Article.asp?Id=21640
    http://www.csdn.net/develop/Read_Article.asp?Id=21641