Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:10:23+00:00 2026-06-06T01:10:23+00:00

I have a php script that watches for the newest json file in a

  • 0

I have a php script that watches for the newest json file in a directory (below). Then in a javascript I parse and display the data from that file. The javascript reloads the data every x minutes.
I won’t have the name of the file just that it will contain json data and to use the newest one.

The PHP script stores the most recent file stored in array fileList[0]. I want to pass that to the javascript without a full page refresh so I checked this stackoverflow post and found that an ajax call can be made to a php script but the example updates an element on the page.

Is there a way to update a javascript variable? I thought of maybe placing the filename as a hidden element and the grabbing before I refresh the data, but that seemed crude.

<?php
date_default_timezone_set('America/New_York');

$files = glob('json/*.*', GLOB_BRACE);
usort($files, 'filemtime_compare');

function filemtime_compare($a, $b)
{
    return filemtime($b) - filemtime($a);  // Order newest to oldest
}

$i = 0;
$show = 1;  // Number of new files to show.
$fileList = array();

foreach($files as $file)
{
    if($i == $show) break; else ++$i;
    array_push($fileList, $file);
}

//echo $fileList[0] // DEBUG: Make sure I have the right file.
?>

AJAX:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
// This will send a request to a PHP page
$.ajax({
var jsonURL = "";
    url: "phpDir.php",
    dataType: "html",
    success: function(data){
        // Place the returned data into your content

    }
    jsonURL = $fileList[0];

});
</script>
</head>
<body>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:10:25+00:00Added an answer on June 6, 2026 at 1:10 am

    To answer your question “Is there a way to update a javascript variable?”

    You would do this:

    var jsonURL = '';
    
    function loadXMLDoc() {
        // This will send a request to a PHP page
        $.ajax({
            url: "phpDir.php",
            dataType: "JSON",
            success: function(data){
                jsonURL = data.url;
            }
        });
    }
    

    For ease of communication I have set the dataType to JSON. So PHP output would need to be json_encoded()ed:

    <?php
        $output = array('url' => $fileList[0]); // or whatever URL you want to return
    
        echo json_encode($output);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that reads in data from an XML file, returns
I have a php script that writes a text file to the same directory
I have a php script that inserts data from an Android app into a
I have a PHP script that executes a .bat file using system(cmd /c C:\dir\file.bat);
I have a PHP script that runs a SELECT query then immediately deletes the
I have a php script that is reading a file in (file_get_contents). I want
I have a PHP script that reads the contents of a file and stores
I have a php script that takes a file's id in the url, finds
I have a PHP script that writes a file to an Azure Blob. Once
I have a php script that takes hierarchical data from a mysql procedure and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.