0

I am posting JavaScript objects to my PHP backend but I am not able to retrieve the data.

I posted this java-script object:

{"name":"Mastermold"}

I am trying to access name like this:

$name= $_POST["name"];

The error is:

{"error":{"type":"ErrorException","message":"Undefined index: name","file":"C:\Program Files (x86)\Ampps\www\app\controllers\JournalsController.php","line":34}}


saveMongoJournal=function(theJournal,doThis) {
                        journalRoute="http://localhost/public/journals"
                        $http.post(journalRoute,theJournal).success(doThis);

                        };

$scope.clicky= function(){
        saveMongoJournal( {name: "mastermold"},alertReply);

                };




$_POST['name'];
kris kaman
  • 101
  • 1
  • 8
  • 2
    You posted it how? *Also, I wish people would comment instead of just downvoting. It's not constructive* – Popnoodles Mar 10 '14 at 00:50
  • 1
    We need informations. How do you post the javascript object, what is the PHP script. – sunshinejr Mar 10 '14 at 00:51
  • 1
    if you have opening double quote, closing quote should be double as well – Sergey Tsibel Mar 10 '14 at 00:53
  • The PHP is fine (except for the syntax error that was in the question). The error shows that `$_POST['name']` is expected to exist. We only need to see how it's trying to be posted. – Popnoodles Mar 10 '14 at 00:53
  • The `$_POST` key (`name`) does not end with the proper quote, it's ending with an apostrophe, so simply fix it by doing `$_POST['name']`. I'm not a JS expert, but I tend not to enclose the object key in quotes, I keep it like `{name: "Mastermold"}`. Not sure if this would make a difference here thought. – aborted Mar 10 '14 at 00:56
  • if you send the javascript object as string like you posted you want to convert it with json_decode to use it as an object in php again, alternatively: check the content of $_POST with var_dump($_POST); and see what is in there and if you correctly send the data – Ke Vin Mar 10 '14 at 00:57
  • Show us the code that does the post. – Stijn de Witt Mar 10 '14 at 01:08
  • This is exactly what I am doing – kris kaman Mar 10 '14 at 01:59
  • the http method is from angular: http://docs.angularjs.org/api/ng/service/$http – kris kaman Mar 10 '14 at 02:04

1 Answers1

2

You may try this (Check this answer)

$data = json_decode(file_get_contents("php://input"));
echo $data->name;
Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307