Bring to PHP a div

Asked

Viewed 52 times

1

I’m unable to bring the contents of a div to PHP with Jquery.

What’s the matter with me?

My code

Html

<form role="form" name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
    <input type="hidden" id="id_oquefizemos" name="id_oquefizemos" class="form-control" value="">
    <div class="col-md-12">
        <div class="card">
            <div class="card-body">

                <div class="form-group">
                    <label class="col-md-12">Título</label>
                    <div class="col-md-12">
                        <input type="text" name="titulo" value="" placeholder="Título Categoria" class="form-control form-control-line">
                    </div>
                </div>
                <div class="form-group">
                    <label for="example-email" class="col-md-12">Imagem</label>
                    <div class="col-md-12">
                        <input type="file" class="form-control form-control-line" name="foto_oquefizemos" id="foto_oquefizemos">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-12">

                        <div class="wmd-panel">
                            <div id="wmd-button-bar-0" class="wmd-button-bar">
                                <div id="wmd-button-row-0" class="btn-toolbar">
                                    <div class="btn-group wmd-button-group1" id="wmd-button-group1-0">
                                        <button class="btn btn-default" id="wmd-bold-button-0" title="" data-original-title="Bold - Ctrl+B">
                                            <i class="fa fa-bold"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-italic-button-0" title="" data-original-title="Italic - Ctrl+I">
                                            <i class="fa fa-italic"></i>
                                        </button>
                                    </div>
                                    <div class="btn-group wmd-button-group2" id="wmd-button-group2-0">
                                        <button class="btn btn-default" id="wmd-link-button-0" title="" data-original-title="Link - Ctrl+L">
                                            <i class="fa fa-link"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-quote-button-0" title="" data-original-title="Blockquote - Ctrl+Q">
                                            <i class="fa fa-quote-left"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-code-button-0" title="" data-original-title="Code Sample - Ctrl+K">
                                            <i class="fa fa-code"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-image-button-0" title="" data-original-title="Image - Ctrl+G">
                                            <i class="fa fa-picture-o"></i>
                                        </button>
                                    </div>
                                    <div class="btn-group wmd-button-group3" id="wmd-button-group3-0">
                                        <button class="btn btn-default" id="wmd-olist-button-0" title="" data-original-title="Numbered List - Ctrl+O">
                                            <i class="fa fa-list-ol"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-ulist-button-0" title="" data-original-title="Bulleted List - Ctrl+U">
                                            <i class="fa fa-list-ul"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-heading-button-0" title="" data-original-title="Heading - Ctrl+H">
                                            <i class="fa fa-header"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-hr-button-0" title="" data-original-title="Horizontal Rule - Ctrl+R">
                                            <i class="fa fa-ellipsis-h"></i>
                                        </button>
                                    </div>
                                    <div class="btn-group wmd-button-group4" id="wmd-button-group4-0">
                                        <button class="btn btn-default" id="wmd-undo-button-0" title="" data-original-title="Undo - Ctrl+Z"
                                            disabled="">
                                            <i class="fa fa-undo"></i>
                                        </button>
                                        <button class="btn btn-default" id="wmd-redo-button-0" title="" data-original-title="Redo - Ctrl+Y"
                                            disabled="">
                                            <i class="fa fa-rotate-right"></i>
                                        </button>
                                    </div>
                                    <div class="btn-group wmd-button-group5 pull-right" id="wmd-button-group5-0">
                                        <button class="btn" id="wmd-help-button-0" title="" data-original-title="Markdown Editing Help">
                                            <i class="fa fa-question"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                            <textarea name="texto" id="wmd-input-0" class="form-control editor wmd-input" rows="10"></textarea>
                            <div id="wmd-preview-0" class="wmd-preview"></div>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-12">
                        <hr>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-12">
                        <input name="cadastrar" type="submit" id="cadastrar" value="Cadastrar Categoria " class="btn btn-default">
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

Jquery

    <script type="text/javascript">
        (function () {

            // $("textarea#pagedownMe").pagedownBootstrap();

            $("textarea#pagedownMeDangerously").pagedownBootstrap({
                'sanitize': false,
                'help': function () { alert("Do you need help?"); },
                'hooks': [
                    {
                        'event': 'preConversion',
                        'callback': function (text) {
                            return text.replace(/\b(a\w*)/gi, "$1");
                        }
                    },
                    {
                        'event': 'plainLinkText',
                        'callback': function (url) {
                            return "This is a link to " + url.replace(/^https?:\/\//, "");
                        }
                    }
                ]
            });





        })();

        $(function(){
   $("#cadastrar").click(function(){
       var conteudo = $("#wmd-preview-0").text(); //<----
       $.post('dicas_categoria.php', {
           texto_descricao: conteudo
       }, function(response){
           // Aqui ficará a lógica que será executada quando sua
           // requisição for enviada e a página PHP te der uma resposta
       });
   });
})



    </script>

Php

<? echo $_POST['nome']; ?>

I tried it but it didn’t work either

$("#form1").submit(function(){ var nome = $('.wmd-preview').html(); }); 
  • <div> does not have val, but content. Exchange val() for html(). It is trivial that you read the documentation of a function before using it.

  • didn’t work yet, I changed how you said val() by html()

  • If you say so nome: conteudo for PHP, why in it you did $_POST['conteudo'] instead of $_POST['nome']?

  • I traded the $_POST['content'] for $_POST['name'] but it didn’t work out

  • So do it in pieces, first make sure that conteudo is receiving the desired value, then make the POST request with a known value and make sure it is coming in PHP, to finally send the content by the request.

  • that would be my question, as I would do in practice?

  • tried so but tbm did not give $("#Form1"). Submit(Function(){ var name = $('. wmd-preview'). html(); });

Show 2 more comments

1 answer

2

It is almost there, but you need to access the text differently, using the method .text() of jquery

$(function(){
   $("#cadastrar").click(function(){
       var conteudo = $("#wmd-preview-0").text(); //<----
       $.post('dicas_categoria.php', {
           nome: conteudo
       }, function(response){
           // Aqui ficará a lógica que será executada quando sua
           // requisição for enviada e a página PHP te der uma resposta
       });
   });
})

More information: http://api.jquery.com/text/

  • How do I take the value to PHP? I used $_POST['content'] and also $_POST['name'], but it didn’t work

  • With $_POST['nome'] should work. Are you sure the post is running? Which jQuery is running? Open your browser console and see if there are no errors.

  • I edited my question exactly how is my form and jquery, could you look if there are any errors? jquery runs yes, because there are other things that jquery works in the same code. In the console no error returns

  • If you are using Chrome, it will open your console, go to Network > XHR and see if you are sending the request... If so, open it and check what data is being sent and what the server response is.

Browser other questions tagged

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