PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Jun 25, 2017 2:02 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun May 14, 2017 3:30 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 319
Hey guys , i have the following update.js file:

Syntax: [ Download ] [ Hide ]
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;
        });
 


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:

Syntax: [ Download ] [ Hide ]
Notice: Undefined index: data in C:\xampp\htdocs\AjaxUser\updateuserinfo.php on line 3
 


My updateuserinfo.php file , looks like so:

Syntax: [ Download ] [ Hide ]
<?php

        $data = $_POST['data'];
        $newUserInfo = json_decode($data, true);
        echo $newUserInfo;

?>
 


what am i doing wrong here ?

Thank you.
Gautam.


Top
 Profile  
 
PostPosted: Sun May 14, 2017 5:05 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6442
Location: WA, USA
You're sending raw data to your script. PHP will not try to interpret that data so you can't use $_POST.

Read the data yourself manually:
Syntax: [ Download ] [ Hide ]
file_get_contents("php://input")


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group