php 将 tinymce textarea 内容保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13660978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:51:48  来源:igfitidea点击:

Saving tinymce textarea content to file

phptinymce

提问by rpd

I have been trying to save tinymce editor textarea content to a .txt file for a while but without success yet. This is my html file code:

我一直在尝试将 tinymce 编辑器 textarea 内容保存到 .txt 文件中,但尚未成功。这是我的 html 文件代码:

<!Doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>TinyMCE example</title>
  <meta name="description" content="HTML5 Basic template">
  <meta name="author" content="R Dickinson">
  <link rel="stylesheet" href="css/styles.css?v=1.0">
  <script src="js/scripts.js"></script>
  <script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
  <script type="text/javascript">
tinyMCE.init({
// General options
mode : "textareas",

});
</script>
  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>
<body>
    <header>
<h1>test tinymce save</h1>
    </header>
    <nav>
    </nav>
    <section>

<form method="post" action="test.php">
    <p> 
        <textarea name="content" style="width:50%"></textarea>
        <input type="submit" value="Save" />
    </p>     
</form>
    </section>
    <aside>
    </aside>
    <footer>
        </footer>
</body>
</html>

Now test.php

现在测试.php

<?php
/*
 * test.php
  */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>test tiny mce</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.21" />
</head>

<body>
<?php

echo(stripslashes($_POST['content']));
?>
<?php
$file = "data.txt";
$fp = fopen($file, 'w');
$data =(stripslashes($_POST['content']));
fwrite($fp, $data);
fclose($fp);
?>

</body>

</html>

I'm grateful for helpful replies to fix this-many thanks :-)

我很感激有帮助的回复来解决这个问题 - 非常感谢 :-)

UpdateFollowing the first answer below I have added triggerSave as:

更新在下面的第一个答案之后,我添加了 triggerSave 为:

 <script language="Javascript">
function submitForm() {
tinyMCE.triggerSave();
document.forms[0].submit();
}
</script>  

and

<form method="post" action="test.php">
<p> 
    <textarea name="content" style="width:50%"></textarea>
    <!--<input type="submit" value="Save" />-->
    <a href="javascript:submitForm();">Submit Form</a>
</p>     

but still no success...More help gratefully received

但仍然没有成功......感谢收到更多帮助

UPDATE 2Here is my jQuery TinyMCE version:

更新 2这是我的 jQuery TinyMCE 版本:

 <!Doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Sample WebPage</title>
  <meta name="description" content="HTML5 Basic template">
  <meta name="author" content="R Dickinson-see sitepoint etc">
  <link rel="stylesheet" href="css/styles.css?v=1.0">
  <script src="js/scripts.js"></script>
  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
        google.load("jquery", "1.3");
</script>
<!-- Load jQuery build -->
<script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
        mode : "textareas"
});
</script>
  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>
<body>
    <header>
        <h1>Enter the main heading, usually the same as the title.</h1>
    </header>
    <nav>
    </nav>
    <section>


<!-- OF COURSE YOU NEED TO ADAPT ACTION TO WHAT PAGE YOU WANT TO LOAD WHEN HITTING "SAVE" -->
<form method="post" action="show.php">
        <p>     
                <textarea name="content" cols="50" rows="15">This is some content that will be editable with TinyMCE.</textarea>
                <input type="submit" value="Save" />
        </p>
</form>

    </section>
    <aside>
    </aside>
    <footer>

  </footer>
</body>
</html>

回答by Yang

The question itself comes down to :

问题本身归结为:

How to save HTML content of TinyMCE into a file.

如何将 TinyMCE 的 HTML 内容保存到文件中。

Well, first of all, you need:

嗯,首先,你需要:

1) Get content of editor
2) Send this content to PHP script
3) Implement some function that would save that content into a file

1) 获取编辑器的内容
2) 将此内容发送到 PHP 脚本
3) 实现一些将该内容保存到文件中的函数

Getting content
Make sure you are getting it the way it should work.

获取内容
确保您以它应有的方式获取它。

For example,

例如,

<script type="text/javascript">

window.onload = function(){

  var btn = document.getElementById('SubmitBtn');

  btn.onclick = function(){

     //This MUST alert HTML content of editor.
     alert( tinyMCE.activeEditor.getContent() );
  }
}

</script>

<input type="button" id="SubmitBtn" value="Get HTML content"/>

Sending content to PHP script

将内容发送到 PHP 脚本

All you need to do is to send value of the method tinyMCE.activeEditor.getContent()to PHP script.

您需要做的就是将方法的值发送tinyMCE.activeEditor.getContent()到 PHP 脚本。

Well, you should use Jquery-AJAXfor that.

嗯,你应该使用Jquery-AJAX它。

Assume that you included jquery.jsinto script tag in the head section, the next step would be sending JavaScript variable to PHP script

假设您包含jquery.js在 head 部分的 script 标签中,下一步是将 JavaScript 变量发送到 PHP 脚本

It would be similar to this one:

这将类似于这个:

<script type="text/javascript">
$(function(){

  var url = "path_to_your_php_script.php";

  $("#SomeSubmitButton").click(function(){
    //"content" will PHP variable 
    $.post(url, { "content" : tinyMCE.activeEditor.getContent() }, function(respond){

       if ( respond == ){
          alert('Content saved to file');
          return;
       } else {
          //Error message assumed
          alert(respond);
        }
    });
  });

});

</script>

PHP script

PHP脚本

Since we were sending "content"this one will be populated in $_POSTsuperglobal

由于我们发送了"content"这个将填充在$_POST超全球

<?php

if ( isset($_POST['content']) ){

   //This is what you want - HTML content from tinyMCE
   //just treat this a string

   if ( save_html_to_file($_POST['content'], '/some_file.html') ){
     //Print 1 and exit script
     die(1);
   } else {
      die('Couldnt write to stream');
   }
}

/**
 * 
 * @param string $content HTML content from TinyMCE editor
 * @param string $path File you want to write into
 * @return boolean TRUE on success
 */
function save_html_to_file($content, $path){
   return (bool) file_put_contents($path, $content);
}

So after execution this you should get back alertwith message of success.

因此,执行此操作后,您应该返回alert成功的消息。

回答by MahanGM

You should update your TinyMCE instances before submitting your form. Use this javascript:

您应该在提交表单之前更新您的 TinyMCE 实例。使用这个javascript:

tinyMCE.triggerSave();

I usually put this in a function and call it on onsubmitevent of form.

我通常把它放在一个函数中,并在onsubmit表单事件中调用它。

There is already different questions around this topic in Stackoverflow.

在 Stackoverflow 中已经有围绕这个主题的不同问题。