Rahul Ok Rahul Ok - 1 year ago 55
HTML Question

$.post in jquery is not working?

My

$.post
is not working as I am trying to extract json data from my php file ,I donno the reason why its not showing the output?
Here's my code.

HTML
:

<pre id="love" type="submit" data-toggle="modal" data-target="#myModal" onclick="mymodal(\''+this["id"]+'\')">'+this["love"]+' students <i class="glyphicon glyphicon-heart fa-lg" style="color:red"></i></pre >


where
this["id"]
has been passed to
mymodal()
function.

javascript code:

var list='';
function mymodal(id){
$.post('postname_.php',{post_id:id},function(data1){
$.each(data1.result,function(){
list+='<div class="modal fade" id="myModal" role="dialog">'+'<div class="modal-dialog">'+'<div class="modal-content">'+
'<div class="modal-header">'+'<button type="button" class="close" data-dismiss="modal">&times;</button>'+
'<h4 class="modal-title">Students who loved your post</h4>'+' </div>'+'<div class="modal-body">';
list+='<h4 class="text-danger">'+this["finame"]+' '+this['sename']+'</h4>';
list+='</div>'+'<div class="modal-footer">'+
'<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>'+'</div>'+
'</div>'+'</div>'+'</div>';
$('#insert').append(list);}),'json'
});
}


my php code:

<?php
require("../vendor/library50-php-5/CS50/CS50.php");
CS50::init(__DIR__ . "/../config.json");

$result=array();
$frows3=CS50::query("SELECT * FROM userhit WHERE plove=? AND bypost=?",1,$_POST['post_id']);
foreach($frows3 as $frow3){
$frow4=CS50::query("SELECT * FROM userinfopersonal WHERE user_id=? ",$frow3['user_id']);
$frow5=CS50::query("SELECT * FROM userprof WHERE user_id=? ",$frow3['user_id']);
array_push($result,array('finame'=>$frow4[0]['firstname'],'sename'=>$frow4[0]['secondname'],'url1'=>$frow5[0]['url']));

}echo json_encode(array('result'=> $result)); ?>


where CS50::query provides me all my rows of table which satisfy the given query ,,no worry abt that ,I checked thats giving perfect json file ..The main problem is,, the
alert
in function
mymodal()
is working that means on clicking pre tag its coming inside function , but the alert inside $.post is not executed.

Answer Source

It looks like your 'json' argument for $.post() function is not within the correct context. See the updated code below (I took the liberty of removing the excess string concatenation – I may be wrong, but it didn't appear to serve a function). Proper spacing can help to catch these syntax errors.

var list='';
function mymodal (id) {
    $.post('postname_.php', {post_id: id}, function (data1) {
        $.each(data1.result, function () {
            list+='<div class="modal fade" id="myModal" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button><h4 class="modal-title">Students who loved your post</h4></div><div class="modal-body">';
            list+='<h4 class="text-danger">' + this["finame"] + ' ' + this['sename'] + '</h4>';
            list+='</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div></div></div>';
            $('#insert').append(list);
        });
    }, 'json');
}

To see the difference, here is the original code tabbed in a more conventional manner.

var list='';
function mymodal(id){
    $.post('postname_.php',{post_id:id},function(data1){
        $.each(data1.result,function(){
            list+='<div class="modal fade" id="myModal" role="dialog">'+'<div class="modal-dialog">'+'<div class="modal-content">'+
            '<div class="modal-header">'+'<button type="button" class="close" data-dismiss="modal">&times;</button>'+
            '<h4 class="modal-title">Students who loved your post</h4>'+' </div>'+'<div class="modal-body">';
            list+='<h4 class="text-danger">'+this["finame"]+' '+this['sename']+'</h4>';
            list+='</div>'+'<div class="modal-footer">'+
            '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>'+'</div>'+
            '</div>'+'</div>'+'</div>';
            $('#insert').append(list);
        }),'json'
    });
}  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download