Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从不同的php文件中获取变量?_Php_Json - Fatal编程技术网

如何从不同的php文件中获取变量?

如何从不同的php文件中获取变量?,php,json,Php,Json,我的index.php页面中有以下代码: // Write the resulting JSON to a text file var jsontext = $('#code-output').text(); $.ajax({ url: 'writetxt.php', type: 'POST', data: { data: jsontext }, success: function(result) { $('#cod

我的
index.php
页面中有以下代码:

// Write the resulting JSON to a text file
    var jsontext = $('#code-output').text();
    $.ajax({
      url: 'writetxt.php',
      type: 'POST',
      data: { data: jsontext },
      success: function(result) {
        $('#code-output').hide().fadeIn('slow');
      }
});
这是
writeText.php
的内容:

// Generating a unique filename using the date and time plus a random 4-digit string
$filename = date("YmdHis-") . rand(1337,9001);

// Making the JSON text file
$jsontext = $_POST["data"];
$fp = fopen("jsontxt/" . $filename . ".txt","w");
fwrite($fp,$jsontext);
fclose($fp);

基本上,每次更改JSON文本时都会创建一个新的文本文件。如何从
index.php
文件中访问$filename?

我假设index.php有一些内容和php变量?使用include将包含该内容。使用会话变量遍历页面


以JSON字符串形式返回$filename的值

echo json_编码(数组('filename'=>$filename)

然后将其从结果变量中拉出

result.filename


(未测试)

在另一个文件中包含该文件

require_once('my_file.php');

echo $varFrom_my_file;
或者将其设置为会话变量

session_start();

$_SESSION['foo'] = "Bar";
然后是另一个文件

echo $_SESSION['foo'];

这是否意味着您希望在每次进行ajax调用时在ajax成功回调函数中返回文件名?是的,这就是我想要做的。然后,正如我所见,您需要以JSON格式返回var值,并通过JS进行处理。请参阅下面的回复。