简体   繁体   中英

Passing json to php and getting response

I am new to php/ajax/jquery and am having some problems. I am trying to pass json to the php file, run some tasks using the json data and then issue back a response. I am using the facebook api to get log in a user,get there details, traslate details to json, send json toe the server and have the server check if the users id already exists in the database. Here is my javascript/jquery

 function checkExisting() {

FB.api('/me', function(response) {
  console.log('Successful login for: ' + response.id );

    var json = JSON.stringify(response);
    console.log(json);

    $.ajax({
            url: "php.php",
            type: "POST",           
            data: {user: json},
            success: function(msg){

                  if(msg === 1){
                     console.log('It exists ' + response.id ); 
                  } else{
                       console.log('not exists ' + response.id      ); 
                  }
                }
        })



});
}

Here is my php file

   if(isset($_POST['user']) && !empty($_POST['user'])) {

$c = connect();
$json = $_POST['user'];
$obj = json_decode($json, true);
$user_info = $jsonDecoded['id'];

$sql = mysql_query("SELECT * FROM user WHERE {$_GET["id"]}");
$count = mysql_num_rows($sql);

if($count>0){
echo 1;
} else{
echo 0; 
} 

 close($c);
}

function connect(){
$con=mysqli_connect($host,$user,$pass);

if (mysqli_connect_errno()) {
  echo "Failed to connect to Database: " . mysqli_connect_error();
 }else{
return $con;
}
 }

 function close($c){
mysqli_close($con);
  }

I want it to return either 1 or 0 based on if the users id is already in the table but it just returns a lot of html tags. . The json looks like so

{"id":"904186342276664","email":"ferrylefef@yahoo.co.uk","first_name":"Taak","gender":"male","last_name":"Sheeen","link":"https://www.facebook.com/app_scoped_user_id/904183432276664/","locale":"en_GB","name":"Tadadadn","timezone":1,"updated_time":"2014-06-15T12:52:45+0000","verified":true} 

Fix the query part:

$sql = mysql_query("SELECT * FROM user WHERE {$_GET['id']}");

Or another way:

$sql = mysql_query("SELECT * FROM user WHERE ". $_GET['id']);

Then it's always better to use dataType in your ajax

    $.ajax({
            url: "php.php",
            type: "POST",           
            data: {user: json},
            dataType: "jsonp", // for cross domains or json for same domain
            success: function(msg){

                  if(msg === 1){
                     console.log('It exists ' + response.id ); 
                  } else{
                       console.log('not exists ' + response.id      ); 
                  }
                }
        })



});

Where is $jsonDecoded getting assigned in your PHP? Looks unassigned to me.

I think you meant to say:

$obj = json_decode($json, true);
$user_info = $obj['id'];

And your SELECT makes no sense. Your referencing $_GET during a POST. Maybe you meant to say:

$sql = mysql_query("SELECT * FROM user WHERE id = {$user_info}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM