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}
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.
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
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
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.