I am new to using Ajax and am getting the success message here but there is an error that the variable is not defined.
//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>
Your code should be like this:
$.ajax({
type: "POST",
url: 'save_pts.php',
data: {TotalPoints : Allpts},
success: function(data)
{
alert (data):
}
});
PHP CODE(save_pts.php)
<?php
$Allpts = "No post value";
if(isset($_POST['TotalPoints'])){
$Allpts = $_POST['TotalPoints'];
}
echo $Allpts;
?>
TotalPoints
is the key of the property. The value is Allpts
, so you need to create a variable with the name Allpts
:
var Allpts = 10;