I'm posting a variable via ajax however when I use it in PHP, I get NAN why is that? it's supposed to be a number
//in js
var variableToSend = 1;
$.post('ajax.php', {variable: variableToSend});
//in php
<?php echo $_POST['variable']?>;
This code won't work,
Because AJAX is something that works in the background, So there is no way the "echo " in PHP will display the data, Because the data returns back to the success:function
and then that can be consoled or printed on the HTML
Here is an example code of how it works:
var variableToSend = 1;
$.ajax({
url:"ajax.php",
method:"POST",
data: {
variable:variableToSend
},
success:function(data){
console.log(data);
}
})
Hope you understood how AJAX works