gpt4 book ai didi

PHP/MySQL/AJAX - 使用 AJAX 刷新查询值

转载 作者:行者123 更新时间:2023-11-29 07:00:39 26 4
gpt4 key购买 nike

我希望我的 header 能够使用数据库中的新值进行刷新。

为了实现它,我创建了一个 AJAX post 方法:

AJAX(已编辑):

$(document).ready( function () {
function update() {
$.ajax({
type: "POST",
url: "indextopgame.php",
data: { id: "<?=$_SESSION['user']['id']?>"},
success: function(data) {
$(".full-wrapper").html(data);
}
});
}

setInterval( update, 5000 );
});

它应该每 10 秒将 $_SESSION['user']['id'] 传递给 indextopgame.php

indextopgame.php 看起来像这样:

PHP 部分(已编辑):

<?php

session_start();

$con = new mysqli("localhost","d0man94_eworld","own3d123","d0man94_eworld");

function sql_safe($s)
{
if (get_magic_quotes_gpc())
$s = stripslashes($s);
global $con;
return mysqli_real_escape_string($con, $s);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$id = trim(sql_safe($_POST['id']));

$data = "SELECT username, email, user_role, fbid, googleid, fname, lname, avatar, energy, energymax, health, healthmax, fame, edollar, etoken, companies, workid, city, function FROM members WHERE id = $id";
$result = mysqli_query($con, $data);

if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {

$_SESSION['user']['user_role'] = $row["id"];
$_SESSION['user']['fbid'] = $row['fbid'];
$_SESSION['user']['googleid'] = $row['googleid'];
$_SESSION['user']['created'] = $row['created'];
$_SESSION['user']['lastlogin'] = $row['lastlogin'];
$_SESSION['user']['username'] = $row['username'];
$_SESSION['user']['fname'] = $row['fname'];
$_SESSION['user']['lname'] = $row['lname'];
$_SESSION['user']['email'] = $row['email'];
$_SESSION['user']['avatar'] = $row['avatar'];
$_SESSION['user']['energy'] = $row['energy'];
$_SESSION['user']['energymax'] = $row['energymax'];
$_SESSION['user']['health'] = $row['health'];
$_SESSION['user']['healthmax'] = $row['healthmax'];
$_SESSION['user']['fame'] = $row['fame'];
$_SESSION['user']['edollar'] = $row['edollar'];
$_SESSION['user']['etoken'] = $row['etoken'];
$_SESSION['user']['companies'] = $row['companies'];
$_SESSION['user']['workid'] = $row['workid'];
$_SESSION['user']['city'] = $row['city'];
$_SESSION['user']['function'] = $row['function'];
}

echo $_SESSION['user']['energy'];
}
}
?>

但这仍然不会用我想要的值更新标题,相反它只会使标题消失。这段代码有什么问题?也许还有其他更有效的方法来刷新 MySQL 中的值?

编辑:

我已经编辑了 AJAX/PHP 代码示例 - 它的工作原理就是这样!但我怎样才能回应所有这些变量呢?一个接一个的回显似乎会再次导致错误,因为值将从我的标题中消失。

编辑2:

解决了,我在语法上犯了一个愚蠢的错误...感谢大家的贡献!

最佳答案

您没有在 ajax 调用中使用从服务器发回的数据:

success: function() { 
$(".full-wrapper").html(data);
}
});

应该是:

success: function(data) { 
^^^^ the returned data
$(".full-wrapper").html(data);
}
});

您还应该检查您的 php 脚本是否确实回显了有用的内容。

关于PHP/MySQL/AJAX - 使用 AJAX 刷新查询值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43607470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com