Join the Stack Overflow Community
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm creating a widget for users to place on their blogs to direct traffic to my coupon code site. I want the widget to access the database and output the 5 top coupons of the day. Here is what I will them place on their site:

<script src="http://example.com/widget/script.js" type="text/javascript"></script>
<div id="example-widget-container"></div>

Now the script.js file looks like:

(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
    script_tag.onload = scriptLoadHandler;
    script_tag.onreadystatechange = function () { // Same thing but for IE
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
            scriptLoadHandler();
        }
    };
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    jQuery(document).ready(function($) { 
        /******* Load CSS *******/
        var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "style.css" 
        });
        css_link.appendTo('head');          

        /******* Load HTML *******/
        var jsonp_url = "http://www.mydomain.com/widget_data.php";
        $.getJSON(jsonp_url, function(data) {
          $('#example-widget-container').html("This data comes from another server: " + data.html);
        });
    });
}

})(); // We call our anonymous function immediately

The problem I'm having is how do I return a json array to the js file, and how do I loop through it to output a unordered list with each coupon being its own list item?

Any help is greatly appreciated!!!!

share|improve this question

Use Ajax,

send a HttpRequest from the widget to the web server, send back from the server a json response, for example in PHP would be something like this

...
//access the database
$sql = "SELECT * FROM coupons LIMIT 5";
while ($row = mysql_fetch_assoc($sql)) {      
  $coupons[] = $row;
}

//return json object
echo json_encode($coupons);
...

Once the js widget has received the json string, you can convert it to a js object to do what you need to

JSON.parse(strJSON)

jQuery ajax request example:

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});
share|improve this answer
    
Can you show me how to make the ajax request? I was initially using jQuery and the .getJSON function, but I really don't like it. Thanks! – PaperChase Sep 28 '11 at 23:28
    
added a jQuery example from jQuery documentation – Packet Tracer Sep 29 '11 at 7:46
    
check jquery documentation --> api.jquery.com/jQuery.ajax – Packet Tracer Sep 29 '11 at 7:46
    
you can use also load() --> api.jquery.com/load – Packet Tracer Sep 29 '11 at 7:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.