0

Here is how I am getting event information from my database into FullCalender, using PHP:

  1. Query database for event information.
  2. Place that information into an array and make my formatting edits, add colors, whatever.
  3. Use json_encode to put array into JSON format.
  4. Write the file to my server as "results.json".

Then in my javascript I use this file to fill my Calendar object:

$('#calendar').fullCalendar({
    events: 'results.json'
});

So that all works great.

Here is my concern:

What happens when I have multiple users?

Jim is going to query the database for his events. Those events are going to be written to results.json.

At the same time, Sue may open the page and query the database for her events. The code is going to overwrite results.json.

Who knows what events are going to show up on their calendar!

I see some suggestions about using socket.io, but there are other articles suggesting that people should be moving to use WebSockets. And I understand that these are supposed to help with real-time applications.

But I'm not following a chat session that is being updated real-time. I have many users that are accessing their own data. Does that mean that every user needs to have their own JSON file? That seems... yucky. Should this file be saved to their local device? That seems full of permission issues.

Any advice?

B. Krafft
  • 147
  • 1
  • 13
  • 2
    why not just directly fire the request which passes the user_id and return the response as a JSON object in the event property? it actually works, have you even tried it? – Roljhon Mar 02 '17 at 16:50
  • I guess here is my issue: I have this JSON object, but how do I pass it into Javascript so that it can be assigned to "events:"? I know saving it as a file on the server works. I also tried hiding the JSON object on my page as a

    and then accessing it from javascript with "document.getElementsByClassName('JSON_result'), but that didn't work. But YES, that would totally solve my problem. How do you do that?

    – B. Krafft Mar 02 '17 at 16:57
  • its still the same thing, you need to feed your json file with some data according to what user is logged in and then pull the file. But since I'm only speaking to what I know of, it actually depends on how you are doing it. Nevertheless, that's actually the whole idea of it, and json file and a json response doesn't have much difference, they will work the same thing. – Roljhon Mar 02 '17 at 17:00
  • Where you save the data to the JSON file, just return it to the browser instead, as plain text. That way you can make an AJAX call from Javascript to get the JSON, and never have to create a file anywhere. I'm guessing that's what @Roljhon is suggesting. – Reinstate Monica Cellio Mar 02 '17 at 17:13
  • @Archer indeed :) – Roljhon Mar 02 '17 at 17:14
  • OH. I've never worked with AJAX before, but I see what you mean. I already have the JSON object at the browser so I will look up a tutorial on AJAX to get it to my Javascript. Thanks for pointing me there. – B. Krafft Mar 02 '17 at 17:55

1 Answers1

1

Many thanks to Roljhon and Archer, who pointed out that I'm asking the wrong question here.

The short answer to the question above is: NO, you should not be saving a file to the server with data that is going to change and be different for each user. There is a better way.

The REAL question is, once I have my data in a PHP variable, how do I get it to JavaScript?

There is a very good explanation here: How to pass variables and data from PHP to JavaScript?

This explains how to use AJAX, or use the DOM in Javascript, or simply echo your PHP variable into a Javascript variable. (Did NOT know you could do that.)

Community
  • 1
  • 1
B. Krafft
  • 147
  • 1
  • 13