简体   繁体   English

在JQuery中调用/获取php变量值

[英]Calling/Getting a php variable value inside JQuery

I am currently learning how jplayer works. 我目前正在学习jplayer的工作原理。 and I've encountered this code 我遇到过这段代码

    <script type="text/javascript">
        $(document).ready(function(){
          $("#jquery_jplayer_1").jPlayer({
            ready: function () {
              $(this).jPlayer("setMedia", {
                m4a: "http://www.jplayer.org/audio/m4a/Miaow-07-Bubble.m4a",
                oga: "http://www.jplayer.org/audio/ogg/Miaow-07-Bubble.ogg"
              });
            },
            swfPath: "js",
            supplied: "m4a, oga"
          });
        });
  </script>

I was wondering how can I get the value of a variable in a php code and insert it here?specifically here. 我想知道如何在php代码中获取变量的值并将其插入此处?具体在这里。

$(this).jPlayer("setMedia", {
                m4a: "http://www.jplayer.org/audio/m4a/Miaow-07-Bubble.m4a",
                oga: "http://www.jplayer.org/audio/ogg/Miaow-07-Bubble.ogg"
              });

inside the m4a: I want to call/insert the value of a php code like $file_path. m4a:里面m4a:我想调用/插入像$ file_path这样的php代码的值。 How can I do that? 我怎样才能做到这一点? since I'll be getting the filepaths from my database and store it in a php variable 因为我将从我的数据库中获取文件路径并将其存储在php变量中

You can just echo it right within the JavaScript, like this: 您可以在JavaScript中直接echo它,如下所示:

<script type="text/javascript">
        $(document).ready(function(){
          $("#jquery_jplayer_1").jPlayer({
            ready: function () {
              $(this).jPlayer("setMedia", {
                m4a: "<?php echo $file_path_m4a; ?>",
                oga: "<?php echo $file_path_oga; ?>"
              });
            },
            swfPath: "js",
            supplied: "m4a, oga"
          });
        });
  </script>

try this 尝试这个

  m4a: "<?php echo $file_path ?>",

if short tag is enable then it would be more readable 如果启用短标记 ,那么它将更具可读性

    <script type="text/javascript">
    $(document).ready(function(){
     var $filepath = "<?=$file_path?>"; //javascript variable can be start with $
      $("#jquery_jplayer_1").jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
            m4a: $filepath+"/filename.mp4",
            oga: $filepath+"/filename.oga",
          });
        },
        swfPath: "js",
        supplied: "m4a, oga"
      });
    });
  </script>

Many has said to just echo the values out. 许多人说只是回应价值观。 The important thing is as long as the file you're working in is interpreted by PHP you can do anything no matter what the file extension is. 重要的是,只要您正在使用的文件由PHP解释,无论文件扩展名是什么,您都可以执行任何操作。 If the file ends in .php your server likely already does this but you could also add .js or .html files to the PHP interpreter. 如果文件以.php结尾,您的服务器可能已经这样做了,但您也可以将.js或.html文件添加到PHP解释器。

<script type="text/javascript">
    $(document).ready(function(){
      $("#jquery_jplayer_1").jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
            m4a: "<?php echo FILE_PATH.filename_mp4;?>",
            oga: "<?php echo FILE_PATH.filename_ogg;?>"
          });
        },
        swfPath: "js",
        supplied: "m4a, oga"
      });
    });

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

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