0

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();
asked Dec 2, 2013 at 20:36
0

3 Answers 3

5

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.

answered Dec 2, 2013 at 20:43

6 Comments

thanks, nontheless your answer and the one from @leemos are also important... :)
I dont know if the e parameter has a message property, read the section for the error parameter in the jquery doc and try this: error: function(data, e){ console.log(e); } (sry for editing that often ^^)
What happens when your Database.php only contains a var_dump($_POST); ? I suspect that there is still this InsertScore function in your php which does not get executed. Remember: the success: function(... part of your javascript has nothing to do with what is going on in your php file. It is just a javascript function which is executed at clientside when the server (Database.php) returns a 200 Ok Header.
I just tried the var_dump but it doesn't show up into the console. So the problem must be in the ajax statement. The Database.php is in the same folder as the js file.
Then try the full url to your Database.php url: "http://yourdomain.com/and/path/to/Database.php",... Something with your paths could be wrong.
|
3

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.

answered Dec 2, 2013 at 20:38

4 Comments

Thanks for your response. I updated the statement with prepared. However, My javascript file doesn't connect with my php file.
@user2827958 Use the absolute path like how you would call it from the browser minus the url, so something like /Database.php.
$.post('Database.php', Score, function(response) { alert(response); I have this now and it returns my the Database.php literally. So with <?php ...?> but it has to process the php
@user2827958 Can you run php files at all?
1

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

answered Dec 3, 2013 at 15:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.