It’s probably easiest to understand by looking at this fiddle:
http://jsfiddle.net/TdBdW/1/ – apologies for the lack of css.
As you can see, for my dynamically generated dropdowns and textboxes I have a big list of hardcoded values. I would have liked to have pulled these straight from a database but the queries take too long to run for them to be used in real-time – i.e. when the user clicks on a drop-down or tries to type in the auto-complete box.
Instead what I would like to do is have the relevant queries run nightly using a cron job but the question is how to take the results of the query and put them into my javascript file.
Anyone have any ideas how to implement this?
Any help is greatly appreciated.
Martin
The best route to achieve what you want (“cron job creates data for use by javascript”) is to go via JSON. This involves:
For simplicity you could create the javascript file using contents similar to the following:
You can easily create this with a static first and last line, and simply add the quoted values from your query in between. You can then load this file into your html document like any other javascript:
When you populate the autocomplete values, simply use a
forloop over this array:The above would be the most basic version of achieving your aims, and you can improve on it, e.g. by: