Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing a webapp which successfully allows me to login to Facebook (I'm using Phonegap and the Phonegap Facebook plugin). I then want to store the logged in users name and ID. To start with as a simple test I wanted to get the following controller to run collect the ID, display it in the xcode console to confirm it was there and then send it to the php code below to then store in a mysql table. I can't seem to get it working and I think it's possibly the format of my data in the {}'s within the $http.post but it's a bit beyond my current knowledge to figure this one out. Any ideas?

function FacebookCtrl($scope) {
    FB.api('/me', function(response) {
       var fbid=response.id;
       console.log('Testing, ' + fbid + '.');
       $http.post('http://somedomain.co.uk/php/users.php', {uid: fbid})
       console.log('Complete');
    });
}

The php code at the receiving end is:

<?php
    $data = file_get_contents("php://input");
    $objData = json_decode($data);

    $uid = $objData->uid;

    try {
        include 'database.php';
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare('INSERT INTO Userdata (oauth_uid) VALUES (:userid)');
    $stmt->execute(array(
            ':userid' => $uid,
    ));
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
?>

The same php code works with another one of my controllers however the difference is that the other controller captures the data passed from a form so the http.post looks like this:

$http.post('http://somedomain.co.uk/php/submitdata.php', {Position1: $scope.position1}

And the code in the php that captures this data is:

$Position1 = $objData->Position1->Name;

As the code all works on another controller, I'm assuming that the issue is with how I'm formatting the data I'm passing between the {}'s?

share|improve this question
1  
if you're using Chrome or Mozilla with firebug, could you please tell what's going on in "Network" tab when you're posting users? –  Maksym H. Feb 18 '13 at 1:22
    
Following your advice I created a web hosted version of the file without the facebook element, but instead simply created a variable that passed the same information through. It worked, so it successfully saved the entry to the database. So I definitely think the issue relates to this line in my controller: $http.post('http://somedomain.co.uk/php/users.php', {uid: fbid}); and these lines in my php: $data = file_get_contents("php://input"); $objData = json_decode($data); $uid = $objData->uid; –  AdrianBorkala Feb 19 '13 at 14:10
    
The problem is that I can't find a way in xcode to see what the response is from the php file. –  AdrianBorkala Feb 19 '13 at 14:56

1 Answer 1

up vote 0 down vote accepted

Try to define success \ error callbacks

$http.post("http://somedomain.co.uk/php/users.php", {uid: fbid})
    .success(function(data, status, headers, config) {
        $scope.data = data;
    }).error(function(data, status, headers, config) {
        $scope.status = status;
    });

What will it say then?

share|improve this answer
    
Thanks for the help. I've done this and added a console.log message for a success and one for a error however I don't get a message for either in the console. I do get a message for a console.log statement that I've added just before the http.post so I can see that the line before is running but it just seems like this line won't run for some reason. –  AdrianBorkala Feb 19 '13 at 21:27
    
Turns out I'm a muppet... Your suggestion to add success/error callbacks has worked in that because I got none, I noticed that I'd not included $http along with $scope in the first line of my controller. Added it and got an instant success. Thanks for helping me get to the right answer. :o) –  AdrianBorkala Feb 19 '13 at 21:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.