Hello i am trying to send fields from a from that i saved in a javascript object to a php server. I am using ajax but when i try to receive the object in php i get a length of 0 when debugging. Basically i can't receive the data. Please what did i do wrong.
Javascript code:
//fields from from saved in an object.
var obj={
'user_name': username,
'pwd': psswd1,
'user_email': email,
'user_phone': mobile,
'sec_quest1': question1,
'ans1': answer1,
'sec_quest2': question2,
'ans2': answer2,
'user_address': address,
'user_userInfo': user_info
};
console.log(obj);
var data = JSON.stringify(obj)
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//document.getElementById("txtHint").innerHTML = xhttp.responseText;
alert(xhttp.responseText);
}
}
xhttp.open("POST", "server.php", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(data);
}
php code:
<?php
$obj = json_decode($_POST["data"]);
echo 'Name: '.sizeof($obj);
?>
It displays a size of zero meaning it's not receiving the data. Please what did i do wrong