Javascript 在 <script> 标签中使用 PHP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8310227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:30:44  来源:igfitidea点击:

Using PHP in <script> tag?

phpjavascriptjsonobject

提问by Gregor Menih

I'm trying to create an object in javascript with PHP. This is my script:

我正在尝试使用 PHP 在 javascript 中创建一个对象。这是我的脚本:

    <script>
        $(document).ready(function(){
            var genres = [
    <?php
    $last = count($userGenres) - 1;
    $i = 0;
    foreach($userGenres as $a){
        $i++;
        echo '{"value":"'.$a['genre'].'", "name":"'.$a['name'].'"}';
        if($i < $last){
            echo ',';
        }
    }
    ?>
                        ];
   });
</script>

When I check the generated source, it creates a valid object, but the whole script in that tag doesn't work now. How would fix this, without JSON?

当我检查生成的源时,它创建了一个有效的对象,但该标记中的整个脚本现在不起作用。如果没有 JSON,如何解决这个问题?

Thanks

谢谢

采纳答案by Lars C. Magnusson

As the comment says you should consider using (php function)json_encode instead, which will turn...

正如评论所说,您应该考虑使用 (php function)json_encode 代替,这将变成...

php:

$a = array(
  array("gender"=> "male", "name"=> "Bob"),
  array("gender"=> "female", "name"=> "Annie")
);

Into json:
[
   {gender:"male", name:"Bob"},
   {gender:"female", name:"Annie"}
]

Echoing json_encode($a) would output it just like that.

回显 json_encode($a) 会像那样输出它。

回答by Kemal Fadillah

You forgot to close the .ready()method and the anonymous function inside of it.

您忘记关闭.ready()方法和其中的匿名函数。

Try this:

尝试这个:

<script>
    $(document).ready(function(){
        var genres = [
            <?php
            $last = count($userGenres) - 1;
            $i = 0;
            foreach($userGenres as $a)
            {
                $i++;
                echo '{"value":"'.$a['genre'].'", "name":"'.$a['name'].'"}';
                if($i < $last)
                {
                    echo ',';
                }
            }
            ?>
        ];
    });
</script>

回答by Bazzz

Your function is not closed properly:

您的功能未正确关闭:

}
    }
    ?>
                        ];
}); // <--- add this
</script>

回答by Mihalis Bagos

This:

这个:

<script>
    $(document).ready(function(){
        var genres = [
            <?php
            $last = count($userGenres) - 1;
            $i = 0;
            foreach($userGenres as $a)
            {
                echo '{"value":"'.$a['genre'].'", "name":"'.$a['name'].'"}';
                if($i < $last)
                {
                    echo ',';
                }

                $i++;
            }
            ?>
        ];
    });
</script>

Changes:You have to close the anonymous function and the ready() method. Also, move the i++ to the end so that you cover all cases (you will be missing one). The error you get is because you have the i++ wrong.

更改:您必须关闭匿名函数和 ready() 方法。此外,将 i++ 移到最后,以便涵盖所有情况(您将丢失一个)。你得到的错误是因为你的 i++ 错误。