如何在不刷新页面的情况下使用 AJAX 发送多个数据


how to send multiple data with ajax without refreshing the page

我想用 php 从数据库中获取 2 个或更多数据并显示在索引页面上。我用这些代码做到了,但我不想使用 2 个 php 页面和 2 个函数。

这是我的索引.php

$(document).ready(function(){
  gonder();
  var int=self.setInterval("gonder()",500);
  var int=self.setInterval("gonder1()",500);
});
function gonder()
{
  $.ajax({
    type:'POST',
    url:'maot.php',
    success: function (msg) {
        $("#maot").html(msg);
        }
   });
}
function gonder1(){
 $.ajax({
    type:'POST',
    url:'durum.php',
    success: function (msg1) {
         $("#durum").html(msg1);
         }
   });
 }

在这里,我使用了 2 个函数,但我无法弄清楚如何在单独的div 上显示 2 个变量。

这里我使用了 2 个 php 页面

硬质合金.php

<?php include ( "dataconn.php" );
$smntnlscada = mysql_query ("Select * FROM kompresor");
$scada = mysql_fetch_assoc($smntnlscada);
$durum = $scada['durum'];
if ($durum==0) {
$durum1='no';
} elseif ($durum==1) {
$durum1='yes';
} elseif ($durum==2) {
$durum1='error';
}
$manuel = $scada['manuel'];
if ($manuel==0) {
$manuel1='oto';
} elseif ($manuel==1) {
$manuel1='manuel';
}

echo $durum1;

?>

毛特.php

<?php include ( "dataconn.php" );
$smntnlscada = mysql_query ("Select * FROM kompresor");
$scada = mysql_fetch_assoc($smntnlscada);
$durum = $scada['durum'];
if ($durum==0) {
$durum1='no';
} elseif ($durum==1) {
$durum1='yes';
} elseif ($durum==2) {
$durum1='error';
}
$manuel = $scada['manuel'];
if ($manuel==0) {
$manuel1='oto';
} elseif ($manuel==1) {
$manuel1='manuel';
}
echo $manuel1;

?>

我的问题是我如何从ajax中获取 2 个变量并在不刷新页面的情况下向它们显示单独的div。感谢您的关注。

在你的

php文件中:

$to_send = array();
array_push($to_send, $durum1, $manuel1);
print json_encode($to_send);

在 JavaScript 成功函数中:

var data = $.parseJSON(msg1);
$("#durum").html(data[0]);
$("#maot").html(data[1]);
function gonder(div){
$.ajax({
    type:'POST',
    url:'getmydiv.php',
    data : 'selecteddiv='+div,
    success: function (msg) {
        $('#'+div).html(msg);
        }
});
}

然后在你的PHP函数中查看$_POST['selecteddiv'],看看你是在做'durum'还是'护城河'并相应地处理。