Load page contents to href for modal

Asked

Viewed 206 times

1

ola personal I started programming in js and jquery currently I am very beginner and I am with a small problem I am trying to show the content of an outside page in a modal but it shows only link of the search and not the content

<div class="modal fade" id="myModal">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
            </button>
             <h4 class="modal-title">Paginas</h4>

        </div>
        <div class="modal-body" id="conteudoModal"></div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
    </div>
</div>

$("a").click(function (e) {
    var id = $(this).attr('id');
    $('#myModal').modal('show');
    $("#conteudoModal").html(this.href);
});

i have another search on this page that works, when I try to use the same code it gives several errors so I tried to do different this is the search

$('#chama').submit(function(e){
  e.preventDefault();
  $.ajax({
    url:'teste_b.php',
    method: 'POST',
    data: { nome_cliente: $('#nome_cliente').val()}, // sua data chegará como $_POST['busca_data'] no PHP.
    success: function(data){                      
        $('#resultado_busca').html(data); // data é o valor printado do lado php
        $('#exampleModalLong').modal('show') ; // abre o modal via jquery
     }
    });                     
  return false; // nao ira para lugar algum 
});

I’m still a beginner and I don’t know exactly why it’s not working

  • 1

    I recommend you research what the function html jQuery do. You put html(this.href), which is basically "show me this URL as HTML".

  • could give me an explanation please and with I fix I gave a read but I did not understand very well

1 answer

0


The method .html(htmlString) Jquery expects to receive an html string that will render.

In your first example, you are passing this.href to him, he will take the content of href, in case the link, and render this text in html.

In your second example, the search, which is already working, you are doing a post with Ajax and the content returning from the POST within date, which is probably an html string, is what you pass to the render method.

To have the same behavior by clicking on a <a> load the contents of this page to a modal, you will need to use the method .load jquery.

Example:

$("a").click(function (e) {
    var id = $(this).attr('id');    
    $("#conteudoModal").load(this.href,
        function(response, status, xhr) {
            if (status === "error") {
                alert('erro');
                console.log(response);
            } else {
                $('#myModal').modal('show'); 
            }           
        });
    return false;
});
  • worked in theory the code rotated but I don’t know what happened now that the modal doesn’t want to appear with the information of the signal that opens inspecting the code but it doesn’t show on the screen

  • @Danielricardo I edited the code to show in the console if there is error in load, test there and see if there is any error in the console

  • i solved error had a jquery on the other page that was inhibiting the modal in this so I tidied up and returned to normal thanks for having explained and helped me

  • Not at all @Danielricardo if the answer helped you consider vote in favor and mark as accepted, thank you!

Browser other questions tagged

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