-1

I want to display data or value return by the ajax function to be store in php variable as mentioned below. Any help?
This is my code

function getUserInfo() 
{
            $.ajax({
                url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + acToken,
                data: null,
                success: function(resp) {
                    user    =   resp;
                    console.log(user);
                    $('#uName').text('Welcome ' + user.name);
                    $('#imgHolder').attr('src', user.picture);
                },
                dataType: "jsonp"
            });
        }


I want to display in php variable as 

$name=$_POST['uName'];
$pics=$_POST['imgHolder'];
echo $name;
echo $pics;
  • You have to look at this. http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – Kannan Rajendran Dec 21 '13 at 07:12
  • Are you you asking how to retrieve POST data IN php, or are you asking how to retrieve POST data from a jQuery $ajax call? Also, what you've written there isn't POST at all -- it's GET. – brandonscript Dec 21 '13 at 07:14
  • Data *returned* by Ajax will be returned to the browser. PHP doesn't run at the browser end. – Quentin Dec 21 '13 at 07:16
  • You've overridden the processing of the HTTP response to treat it as JSONP, but your PHP output anything close to being JSONP. – Quentin Dec 21 '13 at 07:17

3 Answers3

1

I would use PHP to make the request and store the result in an array, since you need to use the result in php

 $acToken='your value';

 $result = file_get_content('https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$acToken);

 echo '<pre>';
 print_r($result);
 echo '<pre>';

make sure to assing a value to $acToken;

Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
0

In your javascript, create ajax post request to your php url:

$.ajax({
    type: "POST",
    url: "some.php",
    data: { uName: "John", imgHolder: "img" }
})
.done(function( msg ) {
    alert( "Data Saved: " + msg );
});

For more information, you can read jQuery AJAX documentation

Andy Librian
  • 911
  • 5
  • 12
0

try below: $name=$_POST['uName']; $pics=$_POST['imgHolder']; echo json_encode(array('name'=>$name,'pics'=>$pics));

as that help for you

james
  • 643
  • 4
  • 24