How to echo JSON sent using javascript in
Posted: Sun May 14, 2017 3:30 am
Hey guys , i have the following update.js file:
and i am sending data to updateuserinfo.php, basically what i want to do is , send the data and then echo the data i have sent in the PHP file. But as of now what is happening is , as soon as i load my index.php file, i get thee following error:
My updateuserinfo.php file , looks like so:
what am i doing wrong here ?
Thank you.
Gautam.
Code: Select all
var xhr = new XMLHttpRequest();
xhr.open('POST' , 'updateuserinfo.php');
xhr.setRequestHeader('Content-Type' , 'application/json');
xhr.onload = function() {
if (xhr.status === 200) {
console.log('done !');
}
}
let data = {
name : 'John',
profession : 'Accountent'
}
xhr.send(JSON.stringify(data));
return false;
});
Code: Select all
Notice: Undefined index: data in C:\xampp\htdocs\AjaxUser\updateuserinfo.php on line 3
Code: Select all
<?php
$data = $_POST['data'];
$newUserInfo = json_decode($data, true);
echo $newUserInfo;
?>
Thank you.
Gautam.