如何在不刷新页面的情况下使用 ajax 发送多个数据
how to send multiple data with ajax without refreshing the page
我想使用 php 从数据库中获取 2 个或更多数据并显示在索引页上。我用这些代码做到了,但我不想使用 2 php 页和 2 个函数。
这是我的index.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 页
durum.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;
?>
maot.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。感谢您的关注。
function gonder(div){
$.ajax({
type:'POST',
url:'getmydiv.php',
data : 'selecteddiv='+div,
success: function (msg) {
$('#'+div).html(msg);
}
});
}
然后在你的 PHP 函数中查看 $_POST['selecteddiv'] 看看你是在做 'durum' 还是 'moat' 并相应地处理。
在您的 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]);
我想使用 php 从数据库中获取 2 个或更多数据并显示在索引页上。我用这些代码做到了,但我不想使用 2 php 页和 2 个函数。
这是我的index.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 页
durum.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;
?>
maot.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。感谢您的关注。
function gonder(div){
$.ajax({
type:'POST',
url:'getmydiv.php',
data : 'selecteddiv='+div,
success: function (msg) {
$('#'+div).html(msg);
}
});
}
然后在你的 PHP 函数中查看 $_POST['selecteddiv'] 看看你是在做 'durum' 还是 'moat' 并相应地处理。
在您的 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]);