The question is, using the php script, I form a certain array variable, say $ a [0] = "1.jpg"; $ a [1] = "test.gif", ..... $ a [200] = "finish. bmp ";. So, here's how to transfer this variable to the js script, for example, is it working like that?

<?php ............................... print ("<script> var massiv =".$a." </script>"); .............................. ?> 

    1 answer 1

    json is mainly used for such tasks:

     <? $a[0]="1.jpg";$a[1]="test.gif";$a[200]="finish.bmp"; ?> <script> var j = <?=json_encode($a)?>; alert(j[0]);//выведет первый элемент массива "1.jpg" </script> 

    upd:

    jQuery:

     <script> $.post('get_img.php',function(a){ alert(a[0]); },'json'); </script> //**get_img.php: <? echo json_encode($a); 

    Js:

     <script src="get_img.php" type="text/javascript"> //**get_img.php: var a=<?=json_encode($a)?>; alert(a[0]); 
    • in the browser I see <script> var j = ["1.jpeg", "879.jpeg", "56.jpeg", "2.jpeg", "3.jpeg"]; alert (j [0]); </ script>. I would like the values ​​to be hidden, that is, ["1.jpeg", "879.jpeg", "56.jpeg", "2.jpeg", "3.jpeg"] from users, or how you can write them in another file, and include <script src> -om? - Vfvtnjd
    • No, you will not hide them, unless you will encrypt (it will also do) - Zowie
    • from just users it is possible through ajax, it is possible and through scripts, but the files will still be visible. - FLK
    • @AlexWindHope, tsya.ru - zavtramen
    • @zavtramen - firstly, Russian is not my main language, and secondly, we are not on the linguists forum - Zowie