如何将tinyMCE编辑器内容保存到mysql

时间:2016-12-27 07:04:10

标签: php mysql

我正在使用tinyMCE编辑器在我的应用中创建帖子,一旦我格式化文本和文本对齐,创建帖子或更新帖子我需要单击保存。当我点击保存时,tinyMCE内容应保存到mysql数据库。我怎么能用PHP做到这一点?

  tinymce.init({
    entity_encoding : "raw",
    selector: '#mytextarea',
    plugins: "table print textcolor colorpicker anchor link searchreplace media emoticons lists visualblocks preview wordcount hr contextmenu",
    mediaembed_service_url: 'SERVICE_URL',
    mediaembed_max_width: 450,
    toolbar: "save print forecolor backcolor anchor link searchreplace emoticons visualblocks preview",
  });

HTML代码

<form method="post">
            <label name="post-title" for="post-title">Post title</label>
            <input type="text" name="post-title" id="post-title"><br>                   
            <label name="post-content" for="post-title">Post Content</label>
            <textarea id="mytextarea"></textarea>
            <br><br>            
            <!-- <input type="submit" class="btn btn-beautuful-blue" name="submit" value="CREATE POST"> -->
        </form>

2 个答案:

答案 0 :(得分:2)

进行以下更改:

<textarea id="mytextarea"></textarea>

to 

<textarea id="mytextarea" name="mytextarea"></textarea>

// provide a name so that we can get its content by using its name

并获得其价值

$content = $_REQUEST['mytextarea'];

现在将$ content的内容保存到数据库

答案 1 :(得分:0)

您必须向表单元素添加操作,假设您要将其发布到文件“post.php”,您将获得<form method="post" action="post.php">。之后,您需要在textarea字段中附加一个名称,我称之为“mytextarea”,因为您在ID(<textarea id="mytextarea"></textarea>)中使用了相同的名称。在文件“post.php”中你需要有一个MySQL连接,我总是用PDO对象制作它们,还有其他的可能性。除此之外,您还需要支票或设置帖子内容。

<强> post.php中

// Checking or there is a post request, and checking or the "mytextarea" field is set
if($_SERVER['request_method'] == 'POST'
&& !empty($_POST['mytextarea']))
{
    // Creating the database connection
    $dsn = 'mysql:dbname=databasename;host=127.0.0.1';
    $user = 'username';
    $password = 'password';

    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }

    // Getting the variables from the form
    $title = isset($_POST['post-title']) : $_POST['post-title'] ? '';
    $content = $_POST['mytextarea'];

    // Executing the query
    $query = $dbh->prepare('INSERT INTO tablename (item_title, item_content) VALUES (:item_title, :item_content)');
    $query->execute([':item_title' => $title, ':item_content' => $content]);
}

<强>链接

http://php.net/manual/en/pdo.construct.php

http://php.net/manual/en/pdo.prepare.php

http://php.net/manual/en/reserved.variables.server.php