Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a couple of ways you can access the data. The first is directly using ADO, a COM object built into Windows that can connect to databases. There are a few drawbacks to this method, the most important being that it's not very secure - your database username and password would have to be stored in plain text in the gadget's script file for anyone to see. This isn't quite as bad with limited permissions set.</p> <p>Most developers would use a combination of PHP and <code>XMLHttpRequest()</code>. The PHP will fetch the data from the database on request, as you suggested yourself. The XMLHttpRequest is what is used for fetching the page contents. The only thing you need to decide is what format to output the data in; XML, JSON or something else. XML and JSON can be parsed by your gadget so either of those is a great choice. If you're running PHP 5.2 or later then <a href="http://uk.php.net/manual/en/function.json-encode.php" rel="nofollow noreferrer">JSON support is native</a>. It's straightforward enough to get the data from the database as an associative array and then <code>json_encode</code> it and print the result.</p> <p><strong>XMLHttpRequest example</strong></p> <pre><code>var xhr = new XMLHttpRequest(); xhr.open("GET", "http://mysite.com/test.php", true); xhr.onreadystatechange = function () { // readyState 4 = complete, status 200 = HTTP OK if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) { parseData(JSON.parse(xhr.responseText)); // parse a JSON response // parseData(xhr.responseXML); // parse an XML response } } xhr.send(); </code></pre> <p>Gadgets running on machines with IE8 installed can use <code>JSON.parse()</code> natively, pre IE8 will need to either <code>eval()</code> the data or run it through a safe parser (most of which <code>eval</code> after making sure the data is valid JSON).</p> <p>More reading:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx" rel="nofollow noreferrer">XMLHttpRequest Object (MSDN)</a><br> <a href="http://www.json.org/js.html" rel="nofollow noreferrer">JSON in JavaScript</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload