I
I
Igor Prishchepa2014-03-16 20:54:34
PHP
Igor Prishchepa, 2014-03-16 20:54:34

How to transfer a variable from a PHP file to a JS file?

The problem is allegedly solved by Google, but for some reason I can not solve it.
I have 2 files, one gives data from the server (php), the other (js) in which I should use the received data. Well, let's say we get something like this (this is an example):

<?php

$data = array(
    array(
        'title' => 'Arduino #1',
        'id' => 1,
        'image' => 'http://www.casemods.ru/templates/images/texts/3_4119479.jpg',
        'top' => 200,
        'left' => 300,
        'width' => 800,
        'height' => 760,
        'points' => array(
            'id' => 1,
            'objectId' => 1,
            'top' => 300,
            'left' => 500,
            'tip' => 500,
        ),
    ),
    array(
        'title' => 'Arduino #2',
        'id' => 2,
        'image' => 'img/objects/arduino.png',
        'top' => 200,
        'left' => 300,
        'width' => 800,
        'height' => 760,
        'points' => array(
            'id' => 1,
            'objectId' => 2,
            'top' => 300,
            'left' => 500,
            'tip' => 500,
        ),
    )
);

echo json_encode($data);

How can I get this $data variable in js file?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
Marina, 2014-03-19
@Kind_pradatoR

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
  $(document).ready(function(){
    $.ajax({
      url: 'http://test_php_json_page/',
      dataType: 'json'
    }).done(function(data){
      $.each(data, function(key, value){
        console.log(key, ' :', value);
      })
    }).fail(function(){
      console.log('error');
    });
  });
</script>

K
Kirill Arutyunov, 2014-03-16
@arutyunov

<?php
$data = 'some_var_for_js';
?>

<html>
<head>
<script type="text/javascript">
var data = <?php echo $data; ?>;
</script>
</head>
...

D
Denis Morozov, 2014-03-16
@morozovdenis

<?php

$data = array(
    array(
        'title' => 'Arduino #1',
        'id' => 1,
        'image' => 'http://www.casemods.ru/templates/images/texts/3_4119479.jpg',
        'top' => 200,
        'left' => 300,
        'width' => 800,
        'height' => 760,
        'points' => array(
            'id' => 1,
            'objectId' => 1,
            'top' => 300,
            'left' => 500,
            'tip' => 500,
        ),
    ),
    array(
        'title' => 'Arduino #2',
        'id' => 2,
        'image' => 'img/objects/arduino.png',
        'top' => 200,
        'left' => 300,
        'width' => 800,
        'height' => 760,
        'points' => array(
            'id' => 1,
            'objectId' => 2,
            'top' => 300,
            'left' => 500,
            'tip' => 500,
        ),
    )
);
?>
<script type="text/javascript">
var data = <?php echo json_encode($data); ?>;
console.log(data[0]["title"]);
</script>

E
Eugene, 2014-03-16
@Nc_Soft

CJavaScript::encode($var)
https://github.com/yiisoft/yii/blob/1.1.14/framewo...

N
Nazar Mokrinsky, 2014-03-16
@nazarpc

There is a wonderful function in PHP called json_encode()

Y
young8junkie, 2014-03-17
@young8junkie

The server (php) cannot send anything, it can only respond. Make a request to the server on the Ajax page and return data with a variable using json. If this answer does not suit you, then you need to improve your knowledge of web development.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question