i want to post a json object to php.
var user = {username:"test", password:"test", name:"test",
email:"test@hotmail.com"};
var str_json = JSON.stringify(user);
$.ajax({
url: '/register_API.php',
type: 'post',
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log('success');
},
data: user
});
}
In php i want to insert it into mysql:
$data = file_get_contents('php://input');
$json = json_decode($data,true);
$username = $json['username'];
$password = $json["password"];
$email = $json['email'];
$insertSql = "INSERT INTO users (username, password, email)
VALUES ('$username', '$password', '$email');";
The $data string contains: username=test&password=test&name=test&email=test%40hotmail.com, but i can't get the variable by decoding...
Thanks in advance!