Imran Aslam February 2016

HTML code not working on jquery call, instead it outputs HTML code

I call a jQuery function on link click and the function uses the post() method to call another page. I expect to see the rendered html result from other page but instead it shows me the html code of the other page.

$(document).ready(function(){
    $('a.thumbnail').click(function(){
        $("div.albums").hide();
        $("div.pics").show();
        var albumid = $(this).siblings('input[name=albumid]').val();
            $.post("gallery-pics-temp.php", { albumid: albumid }, function(data) { 
                $('p#myid').text(data);
            });
        });
    });

The other page: (gallery-pics-temp.php)

<?php 
    if (isset($_POST['albumid'])) {
        echo $_POST['albumid']; //just for checking if albumid is correct
        $openalbumid = $_POST['albumid'];
        include "config.php";
        //$openalbumid = $_GET['albumid'];
        $querypics = $con->prepare("select * from images where img_albumid='$openalbumid'");
        $querypics->execute();
        $resultpics = $querypics->fetchAll();

        $queryaname = $con->prepare("select * from albums where album_id='$openalbumid'");
        $queryaname->execute();
        $resultaname = $queryaname->fetch();
        $aname = $resultaname['album_name']; 
    ?>

    <div class="pics">
        <a href="/" style="float:left;line-height:50px;color:#fff;"> <<< Back </a>
        <h2 align="center" style="color:#fff;"><?php echo $aname; ?></h2>
        <hr />

        <?php foreach ($resultpics as $row1){ ?>
            <div class="col-md-2">
                <a href="#" class="thumbnail" onclick="showImg(<?php echo $row1['img_id']; ?>)">
                    <img src="<?php echo $row1['img_path'];?>/<?php echo $row1['img_id']; ?>.jpg" alt="Pulpit Rock" style="width:200px;height:150px;">  
                </a>
            </di        

Answers


Rory McCrossan February 2016

You need to use the html() method to add the retrieved data to the page, otherwise it gets encoded. Try this:

$('p#myid').html(data);


Vatsal February 2016

Use $('p#myid').html(data) instead It should work

Post Status

Asked in February 2016
Viewed 3,986 times
Voted 9
Answered 2 times

Search




Leave an answer