Javascript 如何使用ajax将php多个值放入不同的div中 这对我来说有点复杂,我仍然是新手。考虑下面的代码块。Jquery的Ajax循环一个php块并将新提要附加到某个div。php返回多个结果,在本例中,它返回“Name”和“lamposon”。在ajax收到结果后,我想附加以下内容作为模板,但其中有php 作为变量,因为每次追加此模板时都会更改。这是html模板 <div id="BuzFeedResult1"> <?php require_once 'php/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)) { ?> <div id="ProfileDiv"> <div> <span class="flat-menu-button"><?php echo $row['name'];?></span> <span class="flat-menu-button"><?php echo $row['slogan'];?> </span> <?php }?> </span> </div> </div> </div>

Javascript 如何使用ajax将php多个值放入不同的div中 这对我来说有点复杂,我仍然是新手。考虑下面的代码块。Jquery的Ajax循环一个php块并将新提要附加到某个div。php返回多个结果,在本例中,它返回“Name”和“lamposon”。在ajax收到结果后,我想附加以下内容作为模板,但其中有php 作为变量,因为每次追加此模板时都会更改。这是html模板 <div id="BuzFeedResult1"> <?php require_once 'php/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)) { ?> <div id="ProfileDiv"> <div> <span class="flat-menu-button"><?php echo $row['name'];?></span> <span class="flat-menu-button"><?php echo $row['slogan'];?> </span> <?php }?> </span> </div> </div> </div>,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,问题。如何在这个div中将上述模板导入ajax中 var $buzfeedresults = $("<div id='BuzFeedResult" + counter + "'></div>"); var$buzfeedresults=$(“”); 然后相应地附加它,就像下面附加“div”一样 var get_fb=(函数(){ var计数器=0; 变量$buzfeed=$(“#buzfeed”); 返回函数(){ $.ajax({ 类型:“POST”, url:“.

问题。如何在这个div中将上述模板导入ajax中

var $buzfeedresults = $("<div id='BuzFeedResult" + counter + "'></div>");
var$buzfeedresults=$(“”);
然后相应地附加它,就像下面附加“div”一样

var get_fb=(函数(){
var计数器=0;
变量$buzfeed=$(“#buzfeed”);
返回函数(){
$.ajax({
类型:“POST”,
url:“../php/topBusinesss\u Algorythm.php”
}).完成(功能(反馈){
计数器+=1;
//正在追加的div。
变量$buzfeedresults=$(“”);
$buzfeedresults.text(反馈);
$buzfeed.append($buzfeedresults);
var$buzfeedDivs=$buzfeed.children('div');
如果($buzfeedDivs.length>10){$buzfeedDivs.last().remove();}
setTimeout(get_fb,4000);
}).fail(函数(jqXhr、textStatus、errorshown){
变量$buzfeedresults=$(“”);
$buzfeedresults.text('Error:'+textStatus);
if(控制台类型!==“未定义”){
console.error(jqXhr、textStatus、errorshown);
}
});
};
})();
获得(fb)
这个问题听起来可能不清楚,请在需要时要求澄清。如果Json更简洁,请推荐一种我可以使用的结构。

您可以通过split()函数和response来实现

var response = "yes|||insert a title...and something more!"
var splitResult=response.split("|||");  
var yesNo=splitResult[0];  
var article=splitResult[1];
alert(yesNo+article);

在分割结果时,更好的解决方案是使用json将结果数据编码到
。/php/topbusiness\u Algorythm.php
中,然后在
done(function(feedback){…})
函数中对结果进行解码,并分别为每个DIV更新HTML内容

您的JS代码示例:

function holdSession() {
  $.ajax({
    type: 'POST',
    url: '../php/TopBusinesses_Algorythm.php',
    success: function(data) {
      var ajax_data = jQuery.parseJSON(data);

      $('#total_count').html(ajax_data.total_count);
      $('#online_count').html(ajax_data.online_count);
      $('#online_users_count').html(ajax_data.online_users_count);
    }
  });

  setTimeout("holdSession()", 30000);
}
var counter = 0;

var get_fb = (function() {
  var $buzfeed = $('#BuzFeed');

  return function() {
    $.ajax({
      url: "../php/TopBusinesses_Algorythm.php"
    }).done(function(feedback) {
      counter += 1;

      //Divs being appended.

      var $buzfeedresults = $("<div id='BuzFeedResult" + counter + "'></div>");

      $buzfeedresults.html(feedback);

      $buzfeed.append($buzfeedresults);

      var $buzfeedDivs = $buzfeed.children('div');

      if ($buzfeedDivs.length > 10) {
        $buzfeedDivs.last().remove();
      }

      setTimeout(get_fb, 4000);
    }).fail(function(jqXhr, textStatus, errorThrown) {
      var $buzfeedresults = $("<div id='BuzFeedError'></div>");

      $buzfeedresults.text('Error: ' + textStatus);

      if (typeof console !== 'undefined') {
        console.error(jqXhr, textStatus, errorThrown);
      }
    });
  };
})();

get_fb();
以及TopU Algorythm.php代码示例:

// here get your data and store it in a array, f.e.:
$result = array();

$result['total_count']        = 1000;
$result['online_count']       = 10;
$result['online_users_count'] = 5;

// finally output result using `json_encode` - {"total_count":1000,"online_count":10,"online_users_count":5}
echo json_encode($result);

第二种解决方案:

您的JS代码:

function holdSession() {
  $.ajax({
    type: 'POST',
    url: '../php/TopBusinesses_Algorythm.php',
    success: function(data) {
      var ajax_data = jQuery.parseJSON(data);

      $('#total_count').html(ajax_data.total_count);
      $('#online_count').html(ajax_data.online_count);
      $('#online_users_count').html(ajax_data.online_users_count);
    }
  });

  setTimeout("holdSession()", 30000);
}
var counter = 0;

var get_fb = (function() {
  var $buzfeed = $('#BuzFeed');

  return function() {
    $.ajax({
      url: "../php/TopBusinesses_Algorythm.php"
    }).done(function(feedback) {
      counter += 1;

      //Divs being appended.

      var $buzfeedresults = $("<div id='BuzFeedResult" + counter + "'></div>");

      $buzfeedresults.html(feedback);

      $buzfeed.append($buzfeedresults);

      var $buzfeedDivs = $buzfeed.children('div');

      if ($buzfeedDivs.length > 10) {
        $buzfeedDivs.last().remove();
      }

      setTimeout(get_fb, 4000);
    }).fail(function(jqXhr, textStatus, errorThrown) {
      var $buzfeedresults = $("<div id='BuzFeedError'></div>");

      $buzfeedresults.text('Error: ' + textStatus);

      if (typeof console !== 'undefined') {
        console.error(jqXhr, textStatus, errorThrown);
      }
    });
  };
})();

get_fb();
var计数器=0;
var get_fb=(函数(){
变量$buzfeed=$(“#buzfeed”);
返回函数(){
$.ajax({
url:“../php/topBusinesss\u Algorythm.php”
}).完成(功能(反馈){
计数器+=1;
//正在追加的div。
变量$buzfeedresults=$(“”);
$buzfeedresults.html(反馈);
$buzfeed.append($buzfeedresults);
var$buzfeedDivs=$buzfeed.children('div');
如果($buzfeedDivs.length>10){
$buzfeedDivs.last().remove();
}
setTimeout(get_fb,4000);
}).fail(函数(jqXhr、textStatus、errorshown){
变量$buzfeedresults=$(“”);
$buzfeedresults.text('Error:'+textStatus);
if(控制台类型!==“未定义”){
console.error(jqXhr、textStatus、errorshown);
}
});
};
})();
获取_fb();
topu Algorythm.php:

<?php 
require_once 'php/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)) {
  echo '<div id="ProfileDiv">';
    echo '<div>';
      echo '<span class="flat-menu-button">'.$row['name'].'</span>';
      echo '<span class="flat-menu-button">'.$row['slogan'].'</span>';
    echo '</div>';
  echo '</div>';
}
?>


没有理解你的意思..更具体一点。我在html的顶部包含了php文件。请提供用php编码这些结果的相关示例。请检查我在回答中添加的示例,希望其足够清楚,如果不清楚,请提问。很好,您忘记了echo json_encode(result)中php代码上的美元符号;不完全,php在单独运行时输出json数据,很好,但您的ajax没有输出任何内容,请检查。我的代码只是示例,您应该根据需要修改它,而且我认为,您不需要将ajax作为POST请求调用。另外,重置var计数器=0是否正常;每一次,get_fb都会被调用?为什么不将整个模板实现到ajax文件中,将包含名称和标语的HTML作为一个结果返回,然后将这个结果附加到div中呢?