我有一个问题,我似乎无法看到我的代码中的错误。我正在尝试使用我的网页上的ajax post函数更新数据库中的变量total
。当我单击按钮但我的数据库未更新时,该功能可以生成具有正确值的警报。这是javascript函数:
function buyeqc(){
var total = $('#eqctotal').val();
$.ajax({
url:"buyeqc.php", //the page containing php script
data: 'total='+total,
type: "POST", //request type
success:function(result){
if (total < "1") {
alert("Please enter a value greater than 0");
} else if (total > "1") {
alert("Thank you for your purchase of "+total+" EQC. Please refresh the page to view your updated balance.");
}
}
});
}
这是它发布到的PHP脚本:
<?php
if (isset($_GET['total'])) {
session_start();
include_once 'dbh.inc.php';
$user = $_SESSION['u_uid'];
$eqcbal = $_SESSION['EQCBal'];
$total = $_GET['total'];
$sql = "UPDATE users SET EQCBal = '$total' WHERE user_uid = '$user';";
mysqli_query($conn, $sql);
}
?>
如果你可以指出我正确的方向,我的错误在哪里,我会很高兴。我觉得这很简单或很小!谢谢。
因为你的php文件中的$ total是NULL,你应该把它改成
`$total = $_POST['total'];`
当您发送ajax发布请求时,数据将存储在$ _POST中
你发帖子请求和php你得到了请求
谢谢你的答案 - 这是$ _GET而不是$ _POST的问题。我也指向我的dbh.inc.php的错误目录。愚蠢的错误:)感谢您的帮助!