我有一个页面可以从数据库中检索两个值(Atender
和Posto
)。
我想刷新textbox1
Atender
值和textbox2
Posto
值。目前,我只能这样做,所以这个功能同时刷新1个值或2个值,但两个值都出现在两个文本框中。
编辑:忘记添加,你看到的PHP就是PainelUtilizador.php。 HTML也是如此,所有代码都在PainelUtilizador.php
中这是我的代码,我确定您在看完之后能够知道我的意思:
<?php
include '../Login/db_login.php';
if(isset($_GET['ajax'])) {
$sql1 = "SELECT Atender,Posto FROM atendercliente WHERE ID=1";
$sql2 = "SELECT Atender,Posto FROM atendercliente WHERE ID=2";
$resultA = $conn->query($sql1);
while($row = $resultA->fetch_assoc()) {
$returnArrayA = array('AtenderA' => $row["Atender"], 'PostoA' => $row["Posto"]);
};
$resultB = $conn->query($sql2);
while($row = $resultB->fetch_assoc()) {
$returnArrayB = array('AtenderB' => $row["Atender"], 'PostoB' => $row["Posto"]);
};
echo json_encode($returnArrayA);
echo json_encode($returnArrayB);exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Painel do utilizador</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
setInterval(function(){
$.ajax('PainelUtilizador.php?ajax=1').done(function(data) {
data = JSON.parse(data);
$("#refreshASenha").val(data['Atender']);
$("#refreshAPosto").val(data['Posto']);
$("#refreshBSenha").val(data['Atender']);
$("#refreshBPosto").val(data['Posto']);
})
}, 1000);
</script>
</head>
<body>
<table border="1">
<tr>
<td> <input type="text" id="refreshASenha" disabled> </td>
<td> <input type="text" id="refreshAPosto" disabled> </td>
</tr>
</table>
<table border="1">
<tr>
<td> <input type="text" id="refreshBSenha" disabled> </td>
<td> <input type="text" id="refreshBPosto" disabled> </td>
</tr>
</table>
</body>
</html>
&#13;
我还是初学者,我自己还在学习AJAX和JQuery
答案 0 :(得分:1)
您需要为ajax&amp;添加支票。然后只调用DB方法。你可以简单地在一个ajax中完成,而不是调用两个
<强>更新强>
<?php
include '../Login/db_login.php';
if(isset($_GET['ajax'])) {
$sql1 = "SELECT Atender,Posto FROM atendercliente WHERE ID=1";
$sql2 = "SELECT Atender,Posto FROM atendercliente WHERE ID=2";
$resultA = $conn->query($sql1);
while($row = $resultA->fetch_assoc()) {
$returnArrayA = array('Atender' => $row["Atender"], 'Posto' => $row["Posto"]);
};
$resultB = $conn->query($sql2);
while($row = $resultB->fetch_assoc()) {
$returnArrayB = array('Atender' => $row["Atender"], 'Posto' => $row["Posto"]);
};
echo json_encode(array('A' => $returnArrayA, 'B' => $returnArrayB));exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Painel do utilizador</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
setInterval(function(){
$.ajax('PainelUtilizador.php?ajax=1').done(function(data) {
data = JSON.parse(data);
$("#refreshASenha").val(data['A']['Atender']);
$("#refreshAPosto").val(data['A']['Posto']);
$("#refreshBSenha").val(data['B']['Atender']);
$("#refreshBPosto").val(data['B']['Posto']);
})
}, 1000);
</script>
</head>
<body>
<table border="1">
<tr>
<td> <input type="text" id="refreshASenha" disabled> </td>
<td> <input type="text" id="refreshAPosto" disabled> </td>
</tr>
</table>
<table border="1">
<tr>
<td> <input type="text" id="refreshBSenha" disabled> </td>
<td> <input type="text" id="refreshBPosto" disabled> </td>
</tr>
</table>
</body>
</html>