php - 在这段 JavaScript 代码中包含变量的正确语法是什么?

标签 php javascript jquery ajax get

我指的是

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/

相关文章:

javascript - 将鼠标悬停在菜单上时在特定 Div 上显示图标

php - MediaWiki 页面标题变量

php - 将 Silverlight 与 PHP 集成

javascript - 如何在javascript中使用ffmpeg设置输出视频的帧率

javascript - 如何使链接在同一选项卡中打开

javascript - 将背景图像添加到 SVG

javascript - 遍历元素以查找特定 ID

PHP/MySql 到 javascript 不工作

javascript - 使用Ajax插入数据但插入需要时间

javascript - 如何在js中拆分多个字符模式?