微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用户通过PHP填写表单后如何自动生成页面?

假设我有一个包含两个输入字段,标题和注释的表单.在用户填写两个字段并提交数据后,将自动创建一个页面,其中包含用户键入的标题和注释,位于根目录中的文件夹中,例如www.root.com/page/将包含自动生成页面.

更新:

我希望将表单数据发送到sql表行,并将自动生成页面的url与标题和注释放在同一个sql行中.

我如何通过PHP完成此操作?

解决方法:

<?PHP
//Template for basic page
$template = <<<EOD
<!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--></title>
</head>

<body>
<!--COMMENT-->
</body>
</html>
EOD;

//handle the posted form
if(isset($_POST['title'])&&isset($_POST['comment'])){
    //replace the areas of the template with the posted values
    $page = str_replace('<!--TITLE-->',htmlentities($_POST['title']),$template);
    $page = str_replace('<!--COMMENT-->',htmlentities($_POST['comment']),$page);
    //create a name for the new page
    $pagename = md5($_POST['title']).'.html';

    //db connect & select
    $db=MysqL_connect('localhost','user','pass');
    MysqL_select_db('yourdb');

    //check if page already exists
    $result = MysqL_query('SELECT pagename from yourtable WHERE url="'.MysqL_real_escape_string($pagename).'"');
    if(MysqL_num_rows($result)>=1){
        $notice = '<p>Page already created <b>./pages/'.$pagename.'</b></p>';
    }else{
        //inset new page into db
        MysqL_query('INSERT into yourtable (`id`,`title`,`comment`,`url`)VALUES("",
        "'.MysqL_real_escape_string(htmlentities($_POST['title'])).'",
        "'.MysqL_real_escape_string(htmlentities($_POST['comment'])).'",
        "'.$pagename.'")');
        //put the created content to file
        file_put_contents('./pages/'.$pagename,$page);
        //make a notice to show the user
        $notice = '<p>New Page created <b>./pages/'.$pagename.'</b></p>';
    }
}
?>

<!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-Language" content="en-gb">

<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make page example</title>
</head>

<body>
<?PHP
//if the notice is set then display it
if(isset($notice)){echo $notice;} ?>
<form method="POST" action="">
  <p>Title:<input type="text" name="title" size="31"></p>
  <p>Comment:</p>
  <p><textarea rows="5" name="comment" cols="21"></textarea></p>
  <p><input type="submit" value="Submit"></p>
</form>
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐