I am trying to use Ajax to send information over to a PHP script to store that information. But when I test it, the data stored at the php side is empty instead of 1234. Can I know what I'm doing wrong please.
//Javascript/ Ajax
document.getElementById('Btn1').onclick = function () {
var num = 1234;
updater(num);
};
function updater(number){
$.ajax({
type: "POST",
url: "http://www.example.com/updt.php",
data: number,
error: function (jqXHR, textStatus, errorThrown) {
alert(" server " + textStatus + ":" + errorThrown)
},
success: function (data) {
alert(data);
}
});
}
//PHP
$myData = $_POST["data"];
echo $myData . "empty.."; //echoes empty.. instead of 1234empty..
Post your data like this
data: {'data':number}, // object
or like this
data:'data='+number // name value pair
instead of
data: number,