0

I need to have this function: an user login using Facebook login. As soon as the user's connected, some Facebook data needs to be sent using the POST method, so another page (setSes.php) can handle the data.

I've created the code below, (added an extra alert to see if the data is loaded as it should, which works just fine), but somehow the page setSes.php is not getting called.

function login() {
       FB.login(function(response) {
        if (response.status === 'connected') {
            document.getElementById('status').innerHTML = 'Connected';

            FB.api('/me', 'GET', {fields: 'first_name,last_name,name,id,link,gender,locale,picture.width(9999).height(9999)'}, function(response) {
                var id=response.id;
                var firstName=response.first_name;
                var lastName=response.last_name;
                var picture=response.picture.data.url;
                alert("Selected ID= "+id);

                $.ajax({
                url:"/setSes.php ",
                method:"POST",

                data:{
                  UID: "id",
                  firstName: "firstName",
                  lastName: "lastName",
                  picture: "picture"
                }
              });
            });
        } else if (response.status === 'not_authorized') {
            document.getElementById('status').innerHTML = 'Not connected';
        } else {
            document.getElementById('status').innerHTML = 'Not able to login';
        }
       }, {scope: 'email'});
   }

setSes.php

<? 
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();

$stmt = $mysqli->prepare("INSERT INTO users(first_name, last_name, oauth_uid, picture
    ) VALUES (?, ?, ?, ?)");
    $stmt->bind_param('ssss', $_POST["firstName"], $_POST["lastName"], $_POST["UID"], $_POST["picture"]);
    $stmt->execute(); 
    $stmt->close();

    echo "I ran";

?>
14
  • 1
    type="post" will do the trick. remove your method Commented Oct 17, 2017 at 19:03
  • nope, unfortunately didn't work :( Commented Oct 17, 2017 at 19:05
  • then go with $.post api.jquery.com/jquery.post Commented Oct 17, 2017 at 19:06
  • 1
    type is an alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. Commented Oct 17, 2017 at 19:08
  • 1
    Open up the network tab on your browser and click the button. You should be able to see both the request to the server for /setSes.php and a response object from the server as to whether or not it was successful. Its unlikely you wont get a response as it seems to be getting into the method so let me know what response you get i.e. 200, 404, 500 etc. Commented Oct 17, 2017 at 19:09

1 Answer 1

1

In the ajax call you're passing strings, not the variables obtained. For your comments, the setSes.phpfile is in the same directory, so try this...

var id = response.id,
    fName = response.first_name,
    lName = response.last_name,
    pic = response.picture.data.url;

$.ajax({
    method: "POST",
    url: "setSes.php",
    data: { UID: id, firstName: fName, lastName: lName, picture: pic },
    success: function(response) {
        alert('SUCCESS: ' + response);
    },
    error: function() {
        alert('ERROR');
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

Is setSes.php in the same directory as the file with the jQuery code?
Yes it is in the same dir.
It gave an error on the $.ajax. Fixed it, now it's working!
Good to hear! What was the error (if you don't mind)?
I copied your code, just copy/paste. But didn't notice that a , was missing after the data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.