用户通过PHP填写表单后如何自动生成页面? [英] How to automatically generate a page after user fills a form via PHP?

查看:163
本文介绍了用户通过PHP填写表单后如何自动生成页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



更新:



我希望将表单数据发送到SQL表行与自动生成页面的URL位于与标题和注释相同的SQL行。



我该如何通过PHP执行此操作?

解决方案

 <?php 
//基本页面的模板
$ template =<< EOD
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/的xhtml1-transitional.dtd>
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title><! - TITLE - >< / title>
< / head>

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

//处理发布的表单
if(isset($ _ POST ['title'])&& isset($ _ POST ['comment'])){
//用发布的值
$ page = str_replace('<! - TITLE - >',htmlentities($ _ POST ['title']),$ template)替换模板的区域;
$ page = str_replace('<! - COMMENT - >',htmlentities($ _ POST ['comment']),$ page);
//为新页面创建一个名称
$ pagename = md5($ _ POST ['title'])。'。html';

// db connect&选择
$ db = mysql_connect('localhost','user','pass');
mysql_select_db('yourdb');

//检查页面是否已经存在
$ result = mysql_query('select pagename from yourtable WHERE url ='。mysql_real_escape_string($ pagename)。'');
if(mysql_num_rows($ result)> = 1){
$ notice ='< p>页面已创建< b> ./ pages /'.$ pagename'< / b> ;< / p为H.';
} else {
//将新页面插入到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。')');
//将创建的内容放入文件
file_put_contents('./ pages /'.$ pagename,$ page);
//发出通知以显示用户
$ notice ='< p>新页面创建< b> ./page/'.$ pagename。'< / b>< / p为H.';
}
}
?>

<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 DTD>
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Languagecontent =en-gb>

< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>创建页面示例< / title>
< / head>

< body>
<?php
//如果通知设置然后显示
if(isset($ notice)){echo $ notice;}?>
< form method =POSTaction =>
< p>标题:< input type =textname =titlesize =31>< / p>
< p>评论:< / p>
< p>< textarea rows =5name =commentcols =21>< / textarea>< / p>
< p>< input type =submitvalue =Submit>< / p>
< / form>
< / body>
< / html>


Let's say I have a form with two input fields, title and comment. After the user fills the two fields and submits the data, a page is automatically created that contains the title and comment that the user typed, in a folder located in root directory, for example, www.root.com/page/ will contain the automatically generated page.

Update:

I'd like the form data to be sent to an SQL table row with the url of the auto-generated page to be in the same SQL row as the title and comment.

How do I do this via 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>

这篇关于用户通过PHP填写表单后如何自动生成页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆