我刚开始使用 Ajax,在这里收到成功消息,但出现变量未定义的错误。
//Variable TotalPoints is already set
$.ajax({
type: "POST",
url: 'save_pts.php',
data: {'TotalPoints' : Allpts},
success: function(data)
{
alert ('Success'):
}
});
<?php // save_pts.php
$Allpts = $_POST['TotalPoints'];
?>
<script>
alert("<?php echo $Allpts ?>");
</script>
最佳答案
你的代码应该是这样的:
$.ajax({
type: "POST",
url: 'save_pts.php',
data: {TotalPoints : Allpts},
success: function(data)
{
alert (data):
}
});
PHP 代码(save_pts.php)
<?php
$Allpts = "No post value";
if(isset($_POST['TotalPoints'])){
$Allpts = $_POST['TotalPoints'];
}
echo $Allpts;
?>
关于使用 Ajax 将 JavaScript 变量传递给 PHP 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24193877/