Solve textarea editing with jQuery

Asked

Viewed 217 times

0

I am making an event registration, with PHP and jQuery, to register it all happens well, but when I change, it causes an error:

Example:
We all know that when we enter a page to edit, we have to have the fields already filled with the database data, this is a fact.

Only when I change the text of textarea, and click on Submit, my database does not change the field, and even trying to show on the console, the new content does not show on the console, shows the database text.

My HTML field:

<textarea name="post_content" id="post_content" class="form-control tinymce"><?=$result->post_content;?></textarea>

This is the variable:

var descricao_edit = j("textarea[name='post_content']").val();

Javascript Script:

<script type="text/javascript">
    var j = jQuery.noConflict();
    j(document).ready(function () {
        var base_url = window.location.origin;
        var btn_enviar = j('#cadastro-dados');

        btn_enviar.on('click', function (event) {
            var base_url = window.location.origin;
            event.preventDefault();

            var categoria = j("input[type=radio][name=post_category]").val();
            var subcategoria = j("#post_category_parent").val();
            var titulo = j("#post_title").val();
            var descricao = j("#post_content").val();
            var id = j("#post_author").val();

            j.ajax({
                url: "<?= INCLUDE_PATH; ?>/modulos/cadastro_evento.php",
                type: 'POST',
                data: 'acao=dados&id=' + id + '&categoria=' + categoria + '&sub=' + subcategoria + '&titulo=' + titulo + '&descricao=' + descricao,
                beforeSend: function () {
                    btn_enviar.attr("disabled", true);
                },
                success: function (data) {
                    j.gritter.add({
                        title: '<i class="fa fa-check"></i> Sucesso!',
                        text: 'Dados registrados com sucesso.',
                        sticky: false,
                        time: ''
                    });

                    btn_enviar.attr("disabled", false);
                    window.location.href = base_url + "/editar/" + data;                    
                },
                error: function (data) {
                    if (data == 'erro') {
                        btn_enviar.attr("disabled", false);
                        alert('Erro ao tentar registrar');
                    }                   
                }
            });
        });
    });
</script>
  • The error comes from server-side code certainly. Normal or ajax submission?

  • good I do with $.ajax();

  • what is that j ? would be the $ ?

  • But I noticed here that not even my registration works, I did a test here, and I was registered, and in the textarea, instead of leaving the text after I changed, to do a test, and when I submitted, registered in the database as standard text, didn’t register with the text that I did crazy thing.

  • Yes, j I am using noConflict(); I did so: var j = jQuery.noConflict();

  • Post the submission by AJAX that you do. To understand better.

  • I just added to the description

  • put it like this: '&descricao=' +encodeURIComponent(descricao) and in all others who may have accentuation

  • Nothing, unsuccessfully, did not work, but the question is no problem in accentuation, just to detail. It’s kind of like you haven’t typed anything in the textarea, and you have a default text registering.

  • I think the problem is how to get the contents of textarea. You are using Tinymce and, I believe, that the value of this editor is not stored directly in the value of the field, so do j("#post_content").val() will always return the initial value and not edited. See here a possible solution.

  • Anderson Carlos Woss, I am also in this your reasoning that it may be the Tinymce, who is giving the stick, because only the textarea field that is with the problem, but I can’t really stop using this blessed rsrs. He is essential in this project.

  • I will try the solution of the Anderson and put results.

  • Anderson, on the console shows me this error: Uncaught Typeerror: tinymce.getContent is not a Function. Maybe this solution is not for the version I use of tinymce that is updated.

  • I did it this way: <pre>var Description = tinymce.getContent('#post_content');</pre>

  • My tinymce code is above this registration function

  • Guys solved, I’ll post here as it was resolved.

  • According to the link that Anderson Carlos Woss gave me, I tried, because the post has several ways to do, and I’ve been trying, so for those who have the same problem goes the stretch that I got. <br/> var Description = tinymce.get('post_content'). getContent(); <br/>Only this can solve. I thank everyone who has been helping me to solve the problem and especially Anderson, for me to inform the solution path. Until next time!

  • I now only have the accent problem, if I leave the code that Everson reported, the accented text gets weird characters, but if I take out the code, it disappears with all text from the letter that contains accent. Then there’s only a piece of text.

Show 13 more comments
No answers

Browser other questions tagged

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