【问题标题】:How can i save the tinyMCE editor content to mysql如何将 tinyMCE 编辑器内容保存到 mysql
【发布时间】:2020-12-18 18:32:46
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 发布后只需获取它并像简单插入一样插入数据库
  • @billynoah 哎呀我的错误。
  • 您要求我们为您编写代码吗?如果没有,您能否更新您的帖子并包含您尝试过的内容?

标签: php mysql


【解决方案1】:

进行以下更改:

<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的内容保存到数据库中

【讨论】:

    【解决方案2】:

    你必须给你的表单元素添加一个动作,假设你想把它发布到文件“post.php”你会得到&lt;form method="post" action="post.php"&gt;。之后,您需要在 textarea 字段中附加一个名称,我将其称为“mytextarea”,因为您在 ID (&lt;textarea id="mytextarea"&gt;&lt;/textarea&gt;) 中使用了相同的名称。在“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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多