简体   繁体   中英

How to show ajax response into bootstrap modal

I want to pop up a modal which will show dynamic data comes from query. so when a user clicked related school will show into modal. I got the response but i'm not getting how can i show into that modal. any suggestion please?

Button:

 <a href="#." data-toggle="modal" data-target="#myModal" data-id="{{$user->id}}"  class="chooseSchoolBtn">Choose School</a>

Ajax :

$(".chooseSchoolBtn").on("click", function (argument) {
    //console.log($(this).data("id"))
    var user_id = $(this).data("id")
    var info = $.get("{{url('school-list')}}", {
        id: user_id
    });
    info.done(function (data) {
        console.log(data)
    });
});

Modal:

<div class="modal-body">
    // This section will come dynamically
    <div class="col-md-2 col-sm-4 col-xs-12">
        <div class="single_school">
            <div class="selection_area">
                <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
            </div>
            <div class="school_img_area" style="background-image: url(school/images/school/school1.jpg);">
            </div>
            <div class="school_info">
                <p class="school_name">School Name</p>
                <p class="school_description">Good School...</p>
                <p class="school_price">12$</p>
            </div>
        </div>
    </div>
    // dynamic section ends
</div>

json response :

{
    id: 1,
    user_id: 2,
    price: "2340.00",
    name: "XYZ",
    display_image: "1510570005ajaPXajmik.png"
}, {
    id: 3,
    user_id: 2,
    price: "123.00",
    name: "ABC",
    display_image: "1510570049QZWiQUh7zY.jpg"
}

Just change the ajax response data

info.done(function(data){          
    $.each(data,function(index,value){
        $(".school_img_area").attr("style","background-image: url(school/images/school/"+value.display_image+");");
        $(".school_name").html(value.name);
        $(".school_price").html(value.price);
    });
});

Happy Coding :-)

$(".chooseSchoolBtn").on("click", function (argument) {
      //console.log($(this).data("id"))
      var user_id = $(this).data("id")
      var info=$.get("{{url('school-list')}}",{id:user_id});
        info.done(function(data){          
              $('DIV WHERE YOU WNAT TO DISPLAY ID').html(data.id);
              // repeat same for user_id, price, so on;
      });
      });

If you want to implement using JSON response than you will iterate through loop and append it html.

For ex -

var html="";
for(var i=0; i<data.length; i++) {
    var html +=<p>data.price</p>data.name<p>;
}
$('.appendTo').html(html);

Other option is in your controller method instead of return the data as JSON you can return HTML and than replace the content when you get ajax success.

$(document).on("click", ".chooseSchoolBtn", function () {
        var schoolId= $(this).data('id');        
        $('#modalId').modal('show');
        $.ajax({
            type: "POST",
            url: "some_url_to_get_school_info",
            data: {'sid':schoolId},
            success: function(data){ 
              $('#div_id_to_show').html(data);
            },
            error: function(){
                alert("Fail")
            }
        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM