Q:

Sending JSON to PHP using ajax, troubles with data

my javascript won't go into my Database.php file.
Anyone knows what's wrong?
I know there is another thread with this question but it just doesn't work for me. 
I have this in javascript
   var Score = 5;

//Score insert
        var postData = 
    {
        "Score":Score
    }
                $.ajax({
                          type: "POST",
                          dataType: "json",
                          url: "Database.php",
                          data: {myData:postData},                    
                          success: function(data){
                                alert('Items added');
                          },
                          error: function(e){
                                console.log(e.message);
                          }
                });         

and this in php
   function InsertScore(){  
    $table = "topscores";

    if(isset($_POST['myData'])){
     $obj = json_encode($_POST['myData']);

     $stmt = $conn->prepare("INSERT INTO " + $table + " VALUES (?)");
     $stmt->bind_param('s', $obj);
     $stmt->execute();
    }
    else{
        console.log("neen");
        }

    $result->close();

A:

change this line
success: function InsertScore(data){

to this
success: function(data){

the success parameter of jquerys ajax method has to be a anonymous function (without a name) or one defined in javascript but definitely not a php function.

A:

You should read up on variable scope, your $table variable is not defined in the scope of your function.
You also have an sql injection problem and should switch to prepared statements with bound variables.

A:

You are trying to send an object to your PHP file instead of a JSON data type.
Try 2 use JSON2 to stringify your object like this :
var scoreINT = 9000;
var usernameSTRING = "testJSON"

var scoreOBJ = {score:scoreINT,username:usernameSTRING};
var jsonData = JSON.stringify(scoreOBJ);

this would give you the following result "{"score":9000,"username":"testJSON"}"
You would be able to send this with your AJAX if you change ( if you follow my variable names ofcourse )
data: {myData:postData}

to
data: {myData:jsonData}

This would already succesfully transfer your data to your PHP file.
regarding your error messages and undefined. the message "e.message" does not exist. so thats the "undefined" you are getting. no worries here.
I noticed the succes and error are called incorrectly. I've just deleted them because there is no need to.
Next. moving up to your PHP.
you would rather like to "DECODE" then to encode your encoded JSON.
you could use the following there :
$score = json_decode($_POST['json'],true);

the extra param true is so you are getting your data into an array ( link )
or you could leave the true so you are working with an object like you already are.
Greetings
ySomic

