简体   繁体   中英

How to to output json into a div

Consider the following PHP Block. I want to output "name" to div #1 and "slog" to div #2

<?php 
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM profiles ORDER BY lastupdated desc limit 1") or die (mysql_error());
while($row = mysql_fetch_array($Result)){
$result = array();
$result['name'] = $row['name'];
$result['slog'] = $row['slog'];
echo json_encode($result);
}
mysql_close($con);
?> 

Here's the ajax that just outputs json itself.

var get_fb = (function() {
    var counter = 0;
    var $buzfeed = $('#BuzFeed');
    return function(){
        $.ajax({
            type: "POST",
            url: "../php/TopBusinesses_Algoriththm.php"
        }).done(function(feedback) {
            counter += 1;
            var $buzfeedresults = $("<div id='BuzFeedResult" + counter + "'></div>").addClass('flat-menu-button');
            $buzfeedresults.text(feedback);
            $buzfeed.append($buzfeedresults);
            var $buzfeedDivs = $buzfeed.children('div');
            if ($buzfeedDivs.length > 10) { $buzfeedDivs.last().remove(); }
            setTimeout(get_fb, 1000);
        })
    };
})();
get_fb();

You can update the html/content of a DOM element with the following:

document.getElementById(name).innerHTML = "text";

So in this instance:

document.getElementById('#1').innerHTML = $yourNameContent;
document.getElementById('#2').innerHTML = $yourSlogContent;

First: move var counter = 0; outside from get_fb() function; Second: Do not call get_fb(); explicitly, because (function(){...})(); already calls method after initialization. Third: Why use POST method if there is not data to submit!

So, do this and have a try:

var counter = 0;
var $buzfeed = $('#BuzFeed');
var get_fb = (function() {
    $.ajax({
        type: "GET",
        url: "../php/TopBusinesses_Algoriththm.php"
    }).done(function(feedback) {
        counter += 1;
        var $buzfeedresults = $("<div id='BuzFeedResult" + counter + "'></div>").addClass('flat-menu-button');
        $buzfeedresults.text(feedback);
        $buzfeed.append($buzfeedresults);
        var $buzfeedDivs = $buzfeed.children('div');
        if ($buzfeedDivs.length > 10) { $buzfeedDivs.last().remove(); }
        setTimeout(get_fb, 1000);
    })
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM