如何在javascript小部件中使用Mysql数据库数据

发布于 2024-12-06 16:12:09 字数 2288 浏览 1 评论 0原文

我正在创建一个小部件,供用户放置在他们的博客上,以将流量引导至我的优惠券代码网站。我希望小部件能够访问数据库并输出当天的 5 个顶级优惠券。这是我将他们放置在他们的网站上的内容:

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

现在 script.js 文件如下所示:

(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

我遇到的问题是如何将 json 数组返回到 js 文件,以及如何循环它以输出无序列表中的每张优惠券都是其自己的列表项?

非常感谢任何帮助!

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!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2024-12-13 16:12:09

使用 Ajax,

从小部件发送 HttpRequest 到 Web 服务器,
从服务器发回 json 响应,例如在 PHP 中会是这样的

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

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

所需的操作:

JSON.parse(strJSON)

一旦 js 小部件收到 json 字符串,您可以将其转换为 js 对象来执行jQuery ajax 请求示例

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

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");
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文