0

I would like to pass an array I have in my PHP file to another file that is written in java script. This is my array:

$pictures = array(
"1" => array("caption" => "1920x1200px", "tag" => "wallpaper", "link" => "#"),
);

And in my java script file this is the place where I want to call the array: (At the place where they shall be in the code I wrote TAG, LINK and CAPTION. Sry if this is a stupid question, but as you see, I have no idea about PHP and java script)

F.helpers.title = {
    beforeShow: function (opts) {
        var text = F.current.title,
            type = opts.type,
            title,
            target;

        if (!isString(text) || $.trim(text) === '') {
            return;
        }

        title = $('<div class="fancybox-title fancybox-title-' + type + '-wrap"><h1>' + text + '</h1><p>CAPTION</p></div><div class="fancybox-title fancydownload" ><a href="LINK"><img src="../../../slider/img/download.png" alt=""/></a></div><div class="fancybox-title fancytag"><h2>TAG</h2></div>');

        switch (type) {
            case 'inside':
                target = F.skin;
            break;

            case 'outside':
                target = F.wrap;
            break;

            case 'over':
                target = F.inner;
            break;

            default: // 'float'
                target = F.skin;

                title
                    .appendTo('body')
                    .width(title.width()) //This helps for some browsers
                    .wrapInner('<span class="child"></span>');

                    //Increase bottom margin so this title will also fit into viewport
                    F.current.margin[2] += Math.abs(     getScalar(title.css('margin-bottom')) );
            break;
        }

        if (opts.position === 'top') {
            title.prependTo(target);

        } else {
            title.appendTo(target);
        }
    }
};
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
Jean
  • 313
  • 2
  • 5
  • 16

5 Answers5

0

Try it using JSON. There are quite a few JSON parsers available

Chris
  • 5,584
  • 9
  • 40
  • 58
0

Encode it as JSON to convert it into a JavaScript literal, then access the resultant value as normal.

var data = <?php echo json_encode(array('foo' => 'bar')); ?>;
console.log(data['foo']);
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

The lifecycle of your PHP script (on the server side) is different from that of JS (on the client side). If you want to pass some information from PHP to client side, you can do one of the following:

  1. You should print this information into the HTML file on the server side itself using your template engine.
  2. You should return this information through another API by converting it into JSON format that is easy to browse through on the javascript side, and call your API using AJAX.
Vikdor
  • 23,934
  • 10
  • 61
  • 84
0

use an ajax call, something like $.post to get the array as a json array, add an echo json_encode($picture) at the end of your PHP script.

dbf
  • 3,278
  • 1
  • 24
  • 34
  • AJAX isn't required if all you want to do is convert the PHP structure to a JavaScript structure in the same page. – Ignacio Vazquez-Abrams Sep 09 '12 at 12:50
  • true if the image really is static, which would completely make it obsolete to use PHP at all and use the array content of PHP in javascript directly. Then there would be no point in using PHP in the first place. – dbf Sep 09 '12 at 12:53
  • Yes, because JavaScript is so good at e.g. accessing databases. Yeesh. – Ignacio Vazquez-Abrams Sep 09 '12 at 12:54
  • And that is suppose to be an argument for using **one** image in a database? – dbf Sep 09 '12 at 12:55
  • Why would you think that there's only one image in the database? – Ignacio Vazquez-Abrams Sep 09 '12 at 12:59
  • Cause I'm a one dimensional person that's had this one handed recursive movement for drinking this one thing called alcohol, and made me realise I didn't really read this one question .. can I buy you a beer for all the finger energy you had to spill in these comments? ;) – dbf Sep 09 '12 at 13:07
  • @dbf Got that. But how do I use the output in my java script file? – Jean Sep 09 '12 at 14:13
  • @Jean, when using an ajax req.? The $.post has a callback method, something like `$.post('file.php',{data:'tosend'},function(response){ console.log(response) }, 'json');`, but I suggest you use the other answers given here – dbf Sep 09 '12 at 14:17
0

PHP just generates files which then you send to the client. JavaScript is executed on the client side. So you should generate in php something like

var pictures = {'1': {caption: '1920x1200px', tag: 'wallpaper', link: '#'}};

and place it in html in script tag for example.

As a variant you may do it this way:

var pictures = <?= json_encode($pictures); ?>;
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79