2017-06-19 104 views
1

我有这个posts.php它使用唯一的id来查看不同的内容。例如:posts.php?id=1显示其中有1,ID的ID在我的表中的所有数据= 2 2,依此类推......返回的AJAX数据不正确

此ID是保持在每个页面上的PHP变种$post_id并以发送在我的jQuery/JavaScript的这个PHP变量,我用:

<div id = "dummy_disp" style = "display: none;"> 
    <?php echo htmlspecialchars($post_id); ?> 
</div> 

<div id = "real_commentsno"></div> 

(我知道,不要做它最好的方式,但它的工作原理)

它连接到我的JS:

var commentid = document.getElementById("dummy_disp"); 
     var commentidreal = commentid.textContent; 

     $.ajax ({ 
     type:"GET", 
     url:"php/post_comments.php", 
     data:'comid='+commentidreal, 
     success: function(data){ 
      $("#real_commentsno").html(data); 
     } 
     }); 

我有这个post_comments.php地方有:

$cc_g_postid = htmlentities($_GET['comid']); 
$cc_postid = mysqli_real_escape_string($con,$cc_g_postid); 


$cc_sql = "SELECT * FROM comments WHERE postuniqueid = '$cc_postid' "; 
$cc_result = mysqli_query($con,$cc_sql); 

$cc_count = mysqli_num_rows($cc_result); 

    echo $cc_count; 

当我尝试与所连接的URL VAR查看post_comments.php旁边:?comid=1页面显示2这是正确的,因为在我的桌子后id=1只有2条评论。但是,当我去我的posts.php ajax显示0,而不是2.我试着看看控制台,没有错误。

有什么我错过或错过了吗?此外,post_comments.php是在位于mypage/php/post_comments.phpposts.php位于mypage/posts.php(我不知道这是否是必要的信息,但也许网址被卡住它还是什么?)

+1

[你看过浏览器开发工具中的AJAX请求/响应了吗?你有没有在项目中包含jQuery库?是否有任何错误报告?你是在网络服务器上运行的吗?](http://jayblanchard.net/basics_of_jquery_ajax.html) –

+3

'data:{comid:commentidreal}' – RiggsFolly

回答

1

的问题是,DIV有周围的ID换行和空格数。你需要删除所有这些空白。

var commentidreal = commentid.textContent.trim(); 
+0

非常感谢!这解决了它。我只是不明白为什么有空白?如果我回显的PHP变种,它不显示任何空白? – Dranreb

+0

在'

...
'块中回应变量,以便您可以看到所有格式。否则,所有空白符合并。 – Barmar

+0

因为在'