Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将tinymce textarea内容保存到文件_Php_Tinymce - Fatal编程技术网

Php 将tinymce textarea内容保存到文件

Php 将tinymce textarea内容保存到文件,php,tinymce,Php,Tinymce,我尝试将tinymce编辑器文本区域内容保存到.txt文件已有一段时间了,但还没有成功。 这是我的html文件代码: <!Doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>TinyMCE example</title> <meta name="description" content="HTML5 Basic templa

我尝试将tinymce编辑器文本区域内容保存到.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>

TinyMCE示例
tinyMCE.init({
//一般选择
模式:“文本区域”,
});
测试tinymce保存

但仍然没有成功……感激地得到了更多的帮助

更新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>

样本网页
load(“jquery”,“1.3”);
tinyMCE.init({
模式:“文本区域”
});
输入主标题,通常与标题相同。

这是一些可以用TinyMCE编辑的内容。


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

tinyMCE.triggerSave();
我通常把它放在一个函数中,并在表单的
onsubmit
事件中调用它


Stackoverflow中已经有关于这个主题的不同问题。

问题本身归结为:

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

首先,你需要:

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

获取内容
确保你得到它的方式应该工作

比如说,

<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"/>
PHP脚本

由于我们发送的是
“内容”
,因此此内容将在
$\u POST
superglobal中填充

<?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);
}

有什么问题?文件是否已创建?是空的吗?服务器是否返回一些错误?$u POST是否已填写?您是否具有该文件的写入权限?问题是我输入文本区域的文本未保存到文本文件中!我的目录中已经有data.txt文件&它具有可写权限。我正在inetpub>wwwroot>的IIS服务器上测试这一点,感谢您的帮助。谢谢您的回复。我不确定你的意思,尤其是当我使用TinyMCE编辑器(不是CKEditor)时。你能用TinyMCE代码再解释一下吗?谢谢!我很抱歉。我看错了你的标题!编辑了答案。感谢您从最初的CKEditor实例中编辑TinyMCE--我已尝试在编辑问题时实现triggerSave,但尚未成功..我看不到您的实现中有任何错误。在这些情况下,我在代码上尝试了不同的方法。为您的表单命名并执行其他操作。尝试
var\u dump()
您的
$\u POST['content']
。我认为一步一步跟踪会有帮助。感谢metal_fan-您的代码提供了一个包含html代码的alertbox,您可以添加代码将tinymce编辑器文本区域中的文本保存到文本文件中(我的原始问题!)。哈,抱歉-我刚刚看到了您的“保存到文件”功能!我将尝试并实施它,并报告回来!干杯:-)我花了一些时间试图解决这个问题,但没有成功。你能用你的代码实现编辑我的2个文件(.html/.php)并发布到这里吗?我非常感谢你的进一步帮助you@rpd您不需要使用
stripslashes()
fopen()
fwrite()
以及最大的错误-如果表单有tinyMCE实例,则不必提交表单。tinyMCE中的HTML内容足够安全(tinyMCE会对其进行过滤),代码会给出一个alertbox。这就是你所需要的,你所要求的。您需要将给定的字符串保存到文件中。对吗?然后,您需要将该字符串发送到PHP脚本并在那里进行处理。感谢您的帮助。我已尝试让您的“写入文件”功能正常工作,但仍然无法正常工作。你能用你的代码实现编辑我的2个原始文件(.html/.php)来帮助我吗?如果你能进一步帮助我,我将非常感激&我感谢你的帮助谢谢
tinyMCE.triggerSave();
<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"/>
<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

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);
}