1

I have a php session file with name 'script1.php' which contains '$position' array. I want to access the value of this array in javascript file. I tried using this "$.get('script1.php', function ( data ) { var x = data;});" command and in 'data' variable i get {"Longitude": 12.917186, "Latitude": 50.831356} this kind of result , but I need to access only integer, how can i access the integer value. I have uploaded the php code. can someone please help me how to solve this problem i am new to the php.

<?php
// Start the session
session_name("mysession");
session_start();
if(isset($_SESSION["i"])){
$i = $_SESSION["i"];
}
else{
$i = 0;
}

// Set session variables
$position = array
(

 array(50.831118,  12.916574),
 array(50.831118,  12.918574), 
 array(50.831356,  12.917186),
 array(50.831604,  12.917834),
 array(50.831763,  12.918172),
 array(50.831916,  12.918603),
 array(50.832825,  12.918413),
 array(50.833206,  12.918303),
 array(50.832825,  12.918413),
 array(50.831916,  12.918603),
 array(50.831763,  12.918172),
 array(50.831604,  12.917834),
 array(50.831356,  12.917186),
 array(50.831118,  12.918574), 
 array(50.831118,  12.916574)

 );

 echo '{"Longitude": '.$position[$i][1].', "Latitude": '.$position[$i]
 [0].'}';

 $i++;
 if($i > count($position)){
 $i = 0;
 }

 $_SESSION["i"] = $i;

JavaScript code

$.get('script1.php', function ( data ) 
{
var x = data;
});

value stored in data variable {"Longitude": 12.917186, "Latitude": 50.831356}

1
  • You need to decode the JSON string to an object like JSON.parse(data) Commented May 30, 2017 at 10:07

3 Answers 3

4

Output the variable you want, remove the other echo.

echo json_encode(array('i' => $_SESSION["i"]));

Use getJSON and access the variable.

$.getJSON('script1.php', function ( data ) 
{
    console.log( data.i );
});

Sidenote: never manually build JSON like you have. Always create a data structure and then JSON encode it because that handles any escaping and special characters.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't get the session-variable in JS. You had to store the needed content in an (hidden) input field or use ajax for it

Comments

0

Rather than manually creating json, you should use phps json_encode on an array/object with the structure you require. You should also set the correct content type header, then jquery will automatically parse the result:

// Start the session
session_name("mysession");
session_start();
if(isset($_SESSION["i"])){
    $i = $_SESSION["i"];
}
else{
    $i = 0;
}

// Set session variables
$position = array
(

 array(50.831118,  12.916574),
 array(50.831118,  12.918574), 
 array(50.831356,  12.917186),
 array(50.831604,  12.917834),
 array(50.831763,  12.918172),
 array(50.831916,  12.918603),
 array(50.832825,  12.918413),
 array(50.833206,  12.918303),
 array(50.832825,  12.918413),
 array(50.831916,  12.918603),
 array(50.831763,  12.918172),
 array(50.831604,  12.917834),
 array(50.831356,  12.917186),
 array(50.831118,  12.918574), 
 array(50.831118,  12.916574)

 );

 $data = array(
     "lat"  => $position[$i][0],
     "long" => $position[$i][1], 
 );

 $i++;
 if($i > count($position)){
 $i = 0;
 }

 $_SESSION["i"] = $i;

header('Content-Type: application/json');
die(json_encode($data));

Then in your javascript you can access data as a regular js object:

$.get('script1.php', function(data){
    console.log(data.lat); //50.831118
    console.log(data.long); //12.916574
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.