Widgets

redleader

Registered
I can make a basic widget thanks to Apples web pages on doing so, but I now want to get my wideget to read and write to a PHP MySQL website / database.

I've read about JavaScript AJAX and PHP MySQL but the penny has not dropped for me yet.

Can anyone pint me to an example or tutorial about how to make a Widget that reads and writs to an MySQL database?

Thank you.
 
Apple has a tutorial on using the XMLHTTPRequest object (what you use to do AJAX).

You are going to do things with your widget in the same manor because it is, more or less, an HTML page.

Now the trick to making it 'Read/Write (do you really want to write?)' a MySQL database is in the PHP file(s) that you create. The widget (using XMLHTTPRequest) calls that PHP script (as if a normal web browser would) passing along parameters to tell the PHP what action to take (you would have to program that action). To get data out of the database and return it to the widget you would need to generate an XML document (make sure you set the content type to text/xml) which can be parsed by the browser and then you, via javascript and the DOM, can get the data out. Writing is as easy as passing parameters along with your request so the PHP script knows what to write.

Sounds complicated but it's not terribly complicated once you figure it out. It just sounds complicated in words. Think of the PHP script as a dynamic web page but instead of generating HTML you are generating XML that can be parsed into the DOM.

Oh also, make sure you enable network access in the widget config file. If you don't you are going to be confused why it works in Safari but not in dashboard... took me a while to figure that one out. I guess that's why they say RTFM.
 
Back
Top