简体   繁体   English

如何在javascript中访问php会​​话文件变量或数据?

[英]How to access php session file variable or data in javascript?

I have a php session file with name 'script1.php' which contains '$position' array. 我有一个名称为'script1.php'的php会话文件,其中包含'$ position'数组。 I want to access the value of this array in javascript file. 我想在javascript文件中访问此数组的值。 I tried using this "$.get('script1.php', function ( data ) { var x = data;});" 我尝试使用此“ $ .get('script1.php',函数(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. 命令并在“数据”变量中,我得到{“经度”:12.917186,“纬度”:50.831356}这种结果,但是我只需要访问整数,我如何访问整数值。 I have uploaded the php code. 我已经上传了php代码。 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 JavaScript代码

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

value stored in data variable {"Longitude": 12.917186, "Latitude": 50.831356} 存储在数据变量{“经度”:12.917186,“纬度”: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并访问变量。

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

Sidenote: never manually build JSON like you have. 旁注:永远不要像您那样手动构建JSON。 Always create a data structure and then JSON encode it because that handles any escaping and special characters. 始终创建一个数据结构,然后对其进行JSON编码,因为它可以处理任何转义和特殊字符。

You can't get the session-variable in JS. 您无法在JS中获得会话变量。 You had to store the needed content in an (hidden) input field or use ajax for it 您必须将所需的内容存储在(隐藏的)输入字段中或对其使用ajax

Rather than manually creating json, you should use phps json_encode on an array/object with the structure you require. 而不是手动创建json,而应在具有所需结构的数组/对象上使用phps json_encode You should also set the correct content type header, then jquery will automatically parse the result: 您还应该设置正确的内容类型标头,然后jquery将自动解析结果:

// 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: 然后在您的JavaScript中,您可以将data作为常规js对象进行访问:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM