【问题标题】:How to automatically generate a page after user fills a form via PHP?用户通过PHP填写表单后如何自动生成页面?
【发布时间】:2011-09-09 19:25:39
【问题描述】:

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

更新:

我希望将表单数据发送到 SQL 表行,其中自动生成页面的 url 与标题和评论位于同一 SQL 行中。

我如何通过 PHP 做到这一点?

【问题讨论】:

  • 您可以这样做,但通常您会将值存储在数据库中,然后在页面加载时获取它们。除非你有实际的理由想要写出文件。

标签: php html forms directory


【解决方案1】:
<?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>

【讨论】:

  • 我希望将表单数据发送到 SQL 表行,其中自动生成页面的 url 与标题和评论位于同一 SQL 行中。
  • 在你的问题中是在哪里说的?
  • 对不起,我只是记得把那个放进去!
  • 4 列:“id”、“title”、“comment”和“url”。
  • 修改了,你明白了。生病留给你休息
猜你喜欢
  • 2013-01-27
  • 2011-08-15
  • 2011-07-18
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 2013-09-20
  • 2020-03-01
相关资源
最近更新 更多