开发者

How do I get a variable from a different php file?

I have the following code in my index.php page:

// 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');
      }
});

And this is the contents of writetxt.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);

Basically, a new text file is created every time a change is made to the JSON text. How do I access $filename from the index.php file?


Either include the file in the other one

require_once('my_file.php');

echo $varFrom_my_file;

or set it to a session variable

session_start();

$_SESSION['foo'] = "Bar";

Then the other file

echo $_SESSION['foo'];


I'm assuming index.php has some content along with PHP variable? Using include will include that content. Use a session variable to go across pages.

PHP Sessions


Return the value of $filename as a JSON string

echo json_encode(array('filename'=>$filename);

Then pull it out of the result var

result.filename

(untested)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜