1

Im new to JSON sintax and I need help to pass some results I've pulled via php from a database, to a JSON code. Here's the code:

<?php
        $id_num = urldecode (intval($_REQUEST['id_cod']));
        $rs = $lista->noticias_testa(1,$id_num);
        $result = mysql_num_rows($rs);

        $rs = $lista->noticiaAll($id_num);
        $result = mysql_num_rows($rs);

        $audio = "files/audios/".mysql_result($rs,0,"audio");
        $title_audio = mysql_result($rs,0,"title_audio");
    ?>

And this JSON to fill a playlist for my mp3 player

<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){

        new jPlayerPlaylist({
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, [
            {
                title:"place to put php variable $title_audio",
                mp3:"path to the music.mp3",                   
            },
        ]);
    });
    //]]>
</script>

I want to pass the php variable "$audio" to the JSON "mp3 path" and title.

Thanks in advance

2 Answers2

1

I'm not sure what are doing but i think that you need json_encode.

It will make your php object/array as json string.

<?php
$json = array('audio' => $audio,
              'title_audio' => $title_audio);

$json = json_encode($json);
?>

Then, you can output it as html/javascript.

<script type="text/javascript">';
    var myObject = <?php echo $json; ?>;
</script>

Now you can access your data in javascript

myObject.audio                 // will contain your audio file
myObject.title_audio           // will contain your number of tracks
marcio
  • 10,002
  • 11
  • 54
  • 83
itodoric
  • 19
  • 2
  • JSON echoed into Javascript source code will be an object literal; `JSON.parse` expects a string. You don't need to `JSON.parse` an object literal at all, just `var myObject = ;` will do just fine. – deceze Jan 22 '15 at 06:10
  • tnx for notice, i was posting in hurry. – itodoric Jan 22 '15 at 12:06
-1

If those two bits of code live within the same .php file, that's pretty straight forward, just go in the javascript part and echo the PHP variables, like this:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){

    new jPlayerPlaylist({
        jPlayer: "#jquery_jplayer_1",
        cssSelectorAncestor: "#jp_container_1"
    }, [
        {
            title:""+<?php echo $title_audio; ?>+"",
            mp3:""+<?php echo $audio; ?>+"",                   
        },
    ]);
});
//]]>

Hint: you could use a even shorter syntax, like:

{ 
    title:""+ <?=$title_audio ?> +"",
    mp3:""+ <?=$audio ?> +"",                 
},
Anaximandro Andrade
  • 1,429
  • 2
  • 11
  • 9
  • Unsafe answer!!! If `$audio` and `$title_audio` are user supplied and come from a database you're vulnerable to XSS. Go to http://stackoverflow.com/a/28078737/438563 instead. – marcio Jan 22 '15 at 06:01
  • This should produce syntax errors in Javascript. – deceze Jan 22 '15 at 06:10
  • marcio: Yes if that data was provided by user, sure he would escape them first.... and deceze: i don't think so , cause the php is processed (server side) before the JavaScript, so when the JavaScript starts it would be only normal strings out there...will verify that – Anaximandro Andrade Jan 23 '15 at 07:10