简体   繁体   English

在此javascript代码中包含变量的正确语法是什么?

[英]what is the proper syntax for including a variable in this javascript code?

The line i am reffering to is 我所依依的那条线是

url:"../php/loadmorecommentsuserpage.php?profilepageuserid=$profilepageuserid&lastComment="+$(".postedComment:last").attr("id"),

i want to send the $profilepageuserid via GET to the loadmorecommentsuserpage.php, but it is not working. 我想通过GET将$ profilepageuserid发送到loadmorecommentsuserpage.php,但是它不起作用。

specifically the value that $profilepageuserid represents is not being sent. 特别是$ profilepageuserid代表的值没有被发送。 so is the syntax wrong? 语法错误吗?

$profilepageuserid is a variable in PHP and i want to send it via JS $ 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>");
                }
            }
        });
    });

});​

Alright. 好的。 This is how PHP interacts with Javascript. 这就是PHP与Javascript交互的方式。 Simply put, it barely does. 简而言之,它几乎没有做到。 PHP is run by the server , which generates text. PHP由服务器运行, 服务器生成文本。 This text is sent to the browser, which runs any Javascript, grabs images, etc. Thankfully, since the Javascript is part of that text, we can cram any data we want into there, including any server state. 该文本被发送到浏览器,该浏览器可运行任何Javascript,获取图像等。值得庆幸的是,由于Javascript是该文本的一部分,因此我们可以向其中填充任何数据,包括服务器状态。

So to initialize a Javascript variable ( var foo ) on the client side (to, say, the current value of $bar ), you will need to cram that data into a Javascript variable declaration. 因此,要在客户端初始化Javascript变量( var foo )(例如,将$bar的当前值更改),则需要将该数据填充到Javascript变量声明中。 What you want the client to see is as follows: 您希望客户看到的内容如下:

var foo = 3; // or whatever else, depending on $bar

So in order to do that, you'll need to echo the value of $foo at the correct time, surrounded by the Javascript syntax: 因此,为了做到这一点,您需要在正确的时间echo$foo的值,并在Javascript语法的包围下:

echo "var foo = $bar;";

Remember that you're merely sending data to the client, to be run (or not run) at a later time. 请记住,您只是向客户端发送数据,以便稍后运行(或不运行)。 You can never "pull" the data back from the browser to the still-running script. 永远无法将数据从浏览器“拉回”到仍在运行的脚本中。 By the time the browser gets the data, the script has finished running and the server has moved on to other useful things, such as enumerating the database. 到浏览器获取数据时,脚本已完成运行,服务器已移至其他有用的功能,例如枚举数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM