How can I recover the data from a textarea that TinyMCE has to format the text

0

I have a textarea to which I apply the TinyMCE text editing plugin ... Now ... when I want to send the content of that textarea to the database, it directly sends it to me null ...

In the Head position the following script (this takes it out of the configuration of the same tinyMCE page)

<script src="https://cloud.tinymce.com/stable/tinymce.min.js?apiKey=ACA_VA_MI_KEY_DE_TINYMCE"></script>
  <script>
    tinymce.init({
    selector:'#resumen',
    language_url : '../../js/es.js'
  });

  tinymce.init({
    selector:'#texto',
    language_url : '../../js/es.js'
  });
</script>

Textareas are called summary and text respectively. Now ... in textareas I have:

<div class="field">
  <label>Resumen / Bajada:</label>
  <textarea id="resumen" name="resumen" required>
  </textarea>
</div>

that "id" what it does is bind me the tinyMCE script

Below all I have a submit button that I send by POST everything that was loaded in the form.

$error = "";
$fecha = $_POST['fecha'];
$titulo = $_POST['titulo'];
$resumen = nl2br($_POST['resumen']);
$texto = nl2br($_POST['texto']);

This I do in the process of saving ... the 'summary' and the 'text' would be the two textareas of which I define in the script

I DO NOT UNDERSTAND WHAT HAPPENS: I went through the problem clean, and did the following:

<head>
<title>My test editor - with tinyMCE and PHP</title>
<script type="text/javascript" src="tinymce/tinymce.min.js"></script>
<script type="text/javascript">
    tinymce.init({
        selector: "#texto",
        language_url : 'es.js',
        plugins: [
          'advlist autolink lists charmap print',
          'searchreplace code',
          'table contextmenu paste code pagebreak'
        ],
        toolbar: 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | pagebreak',
    });

    tinymce.init({
        selector: "#texto2",
        language_url : 'es.js',
        plugins: [
          'advlist autolink lists charmap print',
          'searchreplace code',
          'table contextmenu paste code pagebreak'
        ],
        toolbar: 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | pagebreak',
       });
     </script>
  </head>

  <body>
  <form method="post" action="proceso.php">
    <textarea id="texto" name="texto" rows="15" cols="80"></textarea>
    <textarea id="texto2" name="texto2" rows="15" cols="80"></textarea>

    <br />
    <input type="submit" name="save" value="Submit" />
    <input type="reset" name="reset" value="Reset" />
    </form>
 </body>
 </html>

later in the process.pho I do the following:

<?php
session_start(); //Iniciar una nueva sesión o reanudar la existente
require 'conexion.php';

$allowedTags='<p><strong><em><u><h1><h2><h3><h4><h5><h6><img><li><ol><ul><span><div><br><ins><del><small>';
if($_POST['texto']!='') {
    $sContent = strip_tags(stripslashes($_POST['texto']),$allowedTags);
    echo $sContent;
} else {
    $sContent = "";
}

if($_POST['texto2']!='') {
    $sContent2 = strip_tags(stripslashes($_POST['texto2']),$allowedTags);
    echo $sContent2;
} else {
    $sContent = "";
}

$sql = "INSERT INTO temp (textarea, campo1) VALUES ('$sContent', '$sContent2')";
$resultado = $mysqli->query($sql);

if($resultado) { 
    echo "todo perfecto";
} else{
    echo "error al guardar";
}

?>

now ... it works fine ... save the data in the database, but where it should work, the submit button does not work !!! I click and nothing ... and it's practically the same code

    
asked by MNibor 06.07.2017 в 19:51
source

2 answers

2

To correctly send the text area by PHP you must specify the tags allowed in the content, the details you can see them here . I leave a copy of the example code.

<?php
 $allowedTags='<p><strong><em><u><h1><h2><h3><h4><h5><h6><img>';
 $allowedTags.='<li><ol><ul><span><div><br><ins><del>';  
// Should use some proper HTML filtering here.
  if($_POST['elm1']!='') {
    $sHeader = '<h1>Ah, content is king.</h1>';
    $sContent = strip_tags(stripslashes($_POST['elm1']),$allowedTags);
} else {
    $sHeader = '<h1>Nothing submitted yet</h1>';
    $sContent = '<p>Start typing...</p>';
    $sContent.= '<p><img width="107" height="108" border="0" src="/mediawiki/images/badge.png"';
    $sContent.= 'alt="TinyMCE button"/>This rover has crossed over</p>';
  }
?>
<html>
<head>
<title>My test editor - with tinyMCE and PHP</title>
<script language="javascript" type="text/javascript" src="/js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
  tinyMCE.init({
    theme : "advanced",
    mode: "exact",
    elements : "elm1",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,"
    + "justifyleft,justifycenter,justifyright,justifyfull,formatselect,"
    + "bullist,numlist,outdent,indent",
    theme_advanced_buttons2 : "link,unlink,anchor,image,separator,"
    +"undo,redo,cleanup,code,separator,sub,sup,charmap",
    theme_advanced_buttons3 : "",
    height:"350px",
    width:"600px"
});

</script>
</head>
<body>
 <?php echo $sHeader;?>
 <h2>Sample using TinyMCE and PHP</h2>
 <form method="post" action="<?=$_SERVER['REQUEST_URI']?>">
  <textarea id="elm1" name="elm1" rows="15" cols="80"><?php echo $sContent;?></textarea>
<br />
<input type="submit" name="save" value="Submit" />
<input type="reset" name="reset" value="Reset" />
</form>
</body>
</html>

Another option is to use tinyMCE.triggerSave(); before sending the form. In this link you can see some examples.

    
answered by 07.07.2017 / 20:17
source
0

It all depends on which version of tinyMCE you use.

Assuming you load the tinyMCE with the following code:

    <!DOCTYPE html>
<html>
<head>
  <script src="tinymce.min.js"></script>
  <script>tinymce.init({
  selector: 'textarea',
  plugins: [
    'textcolor colorpicker autoresize'
  ],
   toolbar: 'undo redo | styleselect | bold italic |  forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link'
});</script>

</head>
<body>
  <textarea></textarea>
</body>
</html>

You can use javascript pure :

document.getElementById('mce_0_ifr').contentWindow.document.getElementById('tinymce').innerHTML
    
answered by 06.07.2017 в 20:40