Javascript 从数据库重新加载数据而不刷新页面
我的Ajax成功地更新了数据库中的一些信息,因此他应该更新一个元素,其中显示了这些信息。但事实并非如此。但是,刷新页面后,会导致重新连接到数据库,信息会更新。以下是函数:Javascript 从数据库重新加载数据而不刷新页面,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我的Ajax成功地更新了数据库中的一些信息,因此他应该更新一个元素,其中显示了这些信息。但事实并非如此。但是,刷新页面后,会导致重新连接到数据库,信息会更新。以下是函数: function topUpTheBalance(){ var d = document.getElementById("numberForm").value; $.ajax({ type: 'POST', url: 'handler.php',
function topUpTheBalance(){
var d = document.getElementById("numberForm").value;
$.ajax({
type: 'POST',
url: 'handler.php',
data: {
'topUpBalance':d,
},
success: function () {
var k = document.getElementById("balanceNumber");
k.innerHTML ="Your balance: "+ <?php echo $userRow['userBalance']; ?>;
}
}
);
}
函数topUpTheBalance(){
var d=document.getElementById(“numberForm”).value;
$.ajax({
键入:“POST”,
url:'handler.php',
数据:{
“最高平衡”:d,
},
成功:函数(){
var k=document.getElementById(“余额编号”);
k、 innerHTML=“您的余额:”+;
}
}
);
}
以及handler.php
<?php
ob_start();
session_start();
require_once 'dbconnect.php';
if( !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
}
$res=mysqli_query($link, "SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysqli_fetch_array($res);
//$link = mysqli_connect("localhost","username","password", "users");
$bal = $userRow['userBalance']+$_POST['topUpBalance'];
if($stmt = mysqli_prepare($link, "UPDATE users SET userBalance = ? WHERE userId = ?")){
mysqli_stmt_bind_param($stmt, "di", $bal, $userRow['userId']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $result);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
}
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
<?php ob_end_flush(); ?>
有人能告诉我如何在不刷新页面的情况下更新这些信息吗?问题是,当您编写k.innerHTML=“string”+php代码时,您正试图在客户端运行php 相反,您应该在请求的php文件中生成一个输出,并检索该输出并将其放在这里
该链接解释了如何通过POST向服务器发送数据,以及如何通过get从服务器获取数据 没有详细信息可以帮助您,但我注意到的第一件事是您的AJAX调用,要获取数据,您需要通过get type调用它:
$.ajax({
'url': 'getdata.php',
'type': 'GET',
'dataType': 'json',
'data': {'topUpBalance':d},
'success': function(data) {
// if the request calls properly
},
'error': function(data) {
// if the request fails.
}
});
最重要的是,您应该从php端抛出输出,但尚未发送输出,您应该使用isset($\u POST['topubalance']),然后转到其他进程,例如:-
if(isset($_POST['topUpBalance'])){
$res=mysqli_query($link, "SELECT * FROM users WHERE
userId=".$_SESSION['user']);
$userRow=mysqli_fetch_array($res);
$bal = $userRow['userBalance'] + $_POST['topUpBalance'];
if($stmt = mysqli_prepare($link, "UPDATE users SET userBalance = ?
WHERE userId = ?")){
mysqli_stmt_bind_param($stmt, "di", $bal, $userRow['userId']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $result);
//Send output echo ...... ;
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
}
mysqli_close($link);
}
在jquery端:-
使用参数接收数据
function topUpTheBalance(){
var d = document.getElementById("numberForm").value;
$.ajax({
type: 'POST',
url: 'handler.php',
data: {
'topUpBalance':d,
},
success: function (data) {
// use sent data or unsent data for processing
}
}
);
}
电话在哪里?其中是html中按钮的BalanceNumberRIT调用。HTML文件相当大,所以我不想在这里添加它。但是正如我所说的,ajax完成了他的工作,但是他不能刷新数据,因为数据没有在页面上更新。我在代码中没有看到任何按钮。欢迎使用堆栈溢出。您需要从PHP脚本收集
数据
,以反映该数据并将其添加到DOM中。另外,您的示例没有包含足够的详细信息,无法很好地提供帮助。