How to catch the "name" in an HTML editor?

Asked

Viewed 574 times

1

I am with a question, as I will send by the POST method, what is typed inside an html editor?

É um sistema de notícia em PHP que eu estou criando

I need to send the typing information to the database, but I don’t know how to get the information from the HTML editor. Follow the code:

  <div class="alinha">
    <h3>Sistema de notícias</h3>
    <form action="valida_noticia.php" method="post">
    <div class="form-group">
      <label>Título da notícia</label>
      <input type="text" name="titulonoticia" class="form-control" placeholder="Forneça o título da notícia">
    </div>
    <div class="form-group">
      <script src="https://cdn.ckeditor.com/4.7.3/standard/ckeditor.js"></script>
      <textarea name="editor"></textarea>
      <script>
        CKEDITOR.replace( 'editor' );
      </script>
<!--       <div class="adjoined-bottom">
        <div class="grid-container">
          <textarea id='editor' name='editor'></textarea>
        </div>
      </div> -->
    </div>
    <button type="submit" class="btn btn-default">Enviar</button>
  </div>
</form>
  • Which editor is using and which Javascript code creates it on the page?

  • This is like Ckeditor, but I think we’re missing one <textarea> there.

  • It’s a Ckeditor in a DIV ? but even if you put in a textarea the Ckeditor needs to be instantiated and take the value of the instance, only the post method does not take the value

  • is the CKEDITOR, I’ll confess to you that I am very lay in this matter of HTML editors, I took the editor of an example of them and put on the page that was going to insert news

  • @Pedroribeiro you will send the form via ajax or submit by the same <form> tag ?

  • Sending via POST, I saw several videos on youtube, where people put a code in PHP to instantiate the CKEDITOR, but I reviewed the CKEDITOR documentation I downloaded ( Version 4.6.2 ) and found nothing how to instantiate the same in PHP. The video I’m using to guide me is this: https://www.youtube.com/watch?v=_Khjn-lxRqc

Show 1 more comment

2 answers

2


1 - You cannot put an editor in a div if you wish to submit via form, should put in a textarea:

<textarea id='editor' name='editor'></textarea>

2 - To make it recoverable, you need to start it :

<script>CKEDITOR.replace( 'editor' );</script>

So done, just use the field like any other.

If you want to use AJAX to submit the form you must recover in the following way:

var data = CKEDITOR.instances.editor.getData();

Reference:

<script src="https://cdn.ckeditor.com/4.7.3/standard/ckeditor.js"></script>
  <div class="alinha">
    <h3>Sistema de notícias</h3>
      <form enctype="multipart/form-data"  action="valida_noticia.php" method="post">
         <div class="form-group">
           <label>Título da notícia</label>
           <input type="text" name="titulonoticia" class="form-control" placeholder="Forneça o título da notícia">
         </div>
         <div class="form-group">
           <textarea name="editor"></textarea>
         </div>
         <button type="submit" onclick="updateTexto()" class="btn btn-default">Enviar</button>
      </form>
<script>
   function updateTexto()
   {
    for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[editor].updateElement();
        }
    }

   CKEDITOR.replace( 'editor' );
</script>

Ckeditor Docs

  • the editor does not appear, I did as you said. Only a normal html text area appears

  • @Pedroribeiro Take a look at the code added now at the end there

  • I put all this in place of that div?

  • @Pedroribeiro can put anywhere, the only thing you need is the 'id' of the textarea to be the same as the value of CKEDITOR.replace('here has to be the same'), and also to have imported the library at the beginning of the page.

  • Now it is trimming the editor, but the variable is empty by the post method: string(0) "" NULL

  • Okay, next step, you send this form by a Ubmit button inside <form> or some other way ?

  • boot Submit inside the form, I will edit my code for you to see how this

  • @Pedroribeiro gives a look at the editing, I fixed it in your HTML, and added the function that fills the value, but it’s a kind of old function, see if it works now.

  • Did not work :(. string(0) "" NULL

  • @Pedroribeiro it has been a long time since I use this, tests if this way of the right agr, I added enctype in the form, and the name of the instance, so give I explain why in the answer.

  • You didn’t edit the answer

  • @Pedroribeiro edited yes, it was only two little changes, see <form> tag and javascript function

  • string(0) "" NULL

  • Put this in the php file you receive: var_dump(htmlentities($_POST['editor']));

  • Ue, thus appears: string(29) "<p>TESTING</p> "

Show 11 more comments

0

Complementing what the friend @Antharixbr said, you should use a textarea to use Ckeditor and use the instance to work. To retrieve the data from the textarea to send the form, the methods are:

PHP

$nomedavariavel = $_POST['nomedatextarea'];

JS

var nomedavar = $('textarea[name=noemdatextarea]').val();

Any doubt just call friend!

Browser other questions tagged

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