我指的是
url:"../php/loadmorecommentsuserpage.php?profilepageuserid=$profilepageuserid&lastComment="+$(".postedComment:last").attr("id"),
我想通过 GET 将 $profilepageuserid 发送到 loadmorecommentsuserpage.php,但它不起作用。
特别是 $profilepageuserid 代表的值没有被发送。那么语法错误吗?
$profilepageuserid是PHP中的一个变量,我想通过JS发送它
<script>
$(document).ready(function(){
$('div#text').click(function(){
$("div#text").hide();
$('div#loadMoreComments').show();
$.ajax({
url:"../php/loadmorecommentsuserpage.php?profilepageuserid=$profilepageuserid&lastComment="+$(".postedComment:last").attr("id"),
success:function(html){
if(html){
$("#postedComments").append(html);
$('div#loadMoreComments').hide();
$('div#text').show();
}else{
$('div#box').replaceWith("<div class='box'><center>Finished Loading the Comments</center></div>");
}
}
});
});
});
</script>
最佳答案
$(function() {
//your variable
//if it's from PHP, you need to echo it to a JS variable
var profilepageuserid = '<?= $profilepageuserid ?>'
$('div#text').on('click',function() {
//store required data into variables
//keeps your code readable
var commentid = $(".postedComment:last").attr("id");
$(this).hide();
$('div#loadMoreComments').show();
$.ajax({
url:"../php/loadmorecommentsuserpage.php",
//instead of a very long querystring, just do "data"
data : {
'profilepageuserid' : profilepageuserid,
'lastComment' : commentid
},
success: function(html) {
if (html) {
$("#postedComments").append(html);
$('div#loadMoreComments').hide();
$('div#text').show();
} else {
$('div#box').replaceWith("<div class='box'><center>Finished Loading the Comments</center></div>");
}
}
});
});
});
关于php - 在这段 JavaScript 代码中包含变量的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10136863/