Is it possible to set a trigger to execute the PHP script WHEN there's new data from the JSON?
Yes, that should be possible, just keep in mind that it'd be on the client-side since that's where JS lives
. For that to work you'd need a baseline count of the JSON data so you have the total of events, then x seconds run the PHP script to scrape the data, count that array, compare the two and have the script send the email. No need for a cron
.
Logic-wise, this is how I can see this being done:
1 - On page load, load JSON data array, count records and store result in a global variable.
2 - setInterval() every x seconds/minutes to load a function that reads JSON data array (reason for a function becomes clear below).
3 - In whichever function you have within setInterval() to load the array, store new count in a private variable, then compare the two.
4 - If the private variable is more than global, then there's a new event. AJAX-load the PHP and pass your new event data (at the end of the JSON array) into the URL of the PHP page.
5 - In the PHP mailer page, retrieve that last JSON array data via $_GET request (keep in mind JSON arrays are strings when read in PHP, and need conversion).
6 - Form your email code.
There are another perhaps two ways I can think of this being done, though if you can't tell by the above, how my brain works is a little 'different', lol.