How to edit HTML in a WYSIWYG (Tinymce) editor and save to the bank?

Asked

Viewed 2,038 times

2

I currently have a blog about programming created by me and I’m using the Tinymce editor to create the content. I specifically use Tinymce because it has a free image upload plugin, which makes it much easier to create posts with images, the only problem is that sometimes I need to publish in the content of the post an example HTML snippet and when saving the post everything is fine, But when it comes to editing, Tinymce understands example HTML as "true" HTML and ends up showing the rendered content, not the code.

1 answer

6


The Tinymce editor before being a "rich text" editor is a normal form, so if you want to edit it you must convert the < and > for &lt; and &gt;, as in the example:

<!DOCTYPE html>
<html>
<head>
  <script src="http://cdn.tinymce.com/4/tinymce.min.js"></script>
  <script>tinymce.init({ selector:'#texto' });</script>
</head>
<body>
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$query = 'SELECT postagem FROM tabela WHERE id=1';

if ($result = $mysqli->query($query)) {
    if($row = $result->fetch_assoc()) {
        $variavelDoBanco = $row["postagem"];
    }
}
?>

  <form method="POST" action="arquivo.php">
  <textarea id="texto" name="texto"><?php echo htmlspecialchars($variavelDoBanco); ?></textarea>
  <br>
  <button type="submit">Salvar</button>
  </form>
</body>
</html>

Note if using Laravel with Blade will not be necessary to use htmlspecialchars, for the {$var} already encodes, follows an example:

  • Controller:

    <?php
    namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    
    class HomeController extends Controller
    {
        public function editar()
        {
            return view('editor', [ 'postagem' => '<b>foo</b> bar <s>baz</s>' ]);
        }
    }
    
  • Template editor.blade.php:

      <form method="POST" action="/editar">
      <textarea id="texto" name="texto">{$postagem}</textarea>
      <br>
      <button type="submit">Salvar</button>
      </form>
    

Note being PHP I used the API mysqli the old api mysql_ this discontinued and was removed in PHP7

Old API documentation that informs this:

Useful links:

  • 3

    PLEASE, justify the downvote... is because I answered a question that this in the analysis queue? PLEASE If there’s something wrong, explain it to me so I can get better

  • 1

    I think I got it, the downvote seems fair, I’ll edit... I just wish they’d explain the downvotes, I never really get upset, as long as they explain... I never had a tantrum (returned) because I took downvote when they explain to me why

  • Ready edited, I do not know what language the AP uses, but if edit and inform later I edit with another example. What makes me upset is that the person comes to give the downvote, thinks I’m a person who gets in a tantrum and does not inform me the problem of the answer... Please understand, I am one of the most supportive of the Stack Overflow model, if the downvote is fair I will totally agree and will not be attacking anyone... The idea is always to get better if it’s bad.

  • 3

    I reversed the downvote and gave up, I was leaving for lunch and I forgot to comment, but the answer made no sense with the question before.

  • 2

    @Luishenrique thanks for commenting :) - I think I managed to understand the problem, if there is something else that I can improve or that you think this wrong can negatively and comment, this is part of the system and I promise to try to fix what is possible!

  • Just to complement, I’m using Laravel, but I believe that this information would not be relevant considering the content of the question. So to get the post I use: $post = Post::find($id);

  • @Neimeg will post an example of Laravel, just tell me the version. Note that Lade does not need because it uses a function called e.

  • @Guilhermenascimento, it’s Laravel 5.2

  • @Neimeg I haven’t used Laravel for some time, but I believe it was correct, if something is wrong please let me know.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.