Search code examples
phpjqueryjsonaudiojplayer

Calling/Getting a php variable value inside JQuery


I am currently learning how jplayer works. 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.

$(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. How can I do that? since I'll be getting the filepaths from my database and store it in a php variable


Solution

  • You can just echo it right within the JavaScript, like this:

    <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>