How to automatically generate a page after user fills a form via PHP?

后端 未结 1 1861
借酒劲吻你
借酒劲吻你 2021-01-06 04:49

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 th

1条回答
  •  清酒与你
    2021-01-06 05:33

    
    
    
    
    <!--TITLE-->
    
    
    
    
    
    
    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('',htmlentities($_POST['title']),$template);
        $page = str_replace('',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 = '

    Page already created ./pages/'.$pagename.'

    '; }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 = '

    New Page created ./pages/'.$pagename.'

    '; } } ?> Make page example

    Title:

    Comment:

    0 讨论(0)
提交回复
热议问题