Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Okay so here is my ajax request:

$("#scoreForm").submit(function(e){
    e.preventDefault();
    var nickName = $('#nickName').val();
    var gameScore = parseInt($('#gameScore').text());
    var result = {  'result' : '{ "nick":"'+nickName+'", "score":'+gameScore+' }' };
    //var result = { "nick":nickName, "score":gameScore };
    $.ajax({
        url: "http://localhost:8888/snake/addScore.php",
        type: "POST",
        data: result,
        //dataType: "jsonp",
        success: function(data){
            alert("siker");

        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("bukta " + textStatus);
            //console.log(data);
        }
    });
    return false;
});

and my php process code:

$json_decoded = json_decode($_POST['result'],true);
//$nick = $_GET['nick'];
//$score = $_GET['score'];

$mysqli = new mysqli("localhost", "root", "root", "snake",8889);
//$mysqli->query("INSERT INTO scores(nickName,score) VALUES('".$nick."', ".$score.")");
$mysqli->query("INSERT INTO scores(nickName,score) VALUES('".$json_decoded['nick']."', ".$json_decoded['score'].")");

echo "true";

Now i got the data inserted into the database, but ajax still firing error event. i read that if i set the dataType to jsonp it will go trough , but then i get parse error, how do i get past that?

share|improve this question
1  
What error are you getting? – Joshua Dwire Nov 1 '12 at 18:38
2  
Always escape your SQL INSERTS!!!!!! – Svetlio Nov 1 '12 at 18:47
i'm just trying things first, don't worry :) – Sándor Tamás Nov 1 '12 at 18:52

2 Answers

when you request using http request the params from browser and get params on your server must be same, but in your code i don't see params from your client

$.ajax({
        url: "http://localhost:8888/snake/addScore.php",
        type: "POST",
        data: result,
        //dataType: "jsonp",
        success: function(data){
            alert("siker");

        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("bukta " + textStatus);
            //console.log(data);
        }
    });

this is the point data: result, and you get params result on your server with this $_POST['result']

try to add result params on your client here will be

$.ajax({
        url: "http://localhost:8888/snake/addScore.php",
        type: "POST",
        data: 'result=' + result,
        //dataType: "jsonp",
        success: function(data){
            alert("siker");

        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("bukta " + textStatus);
            //console.log(data);
        }
    });
share|improve this answer

Wehn you access the $_POST variables in your php script, you need to refer to them the way they are packaged with the JSON object:

$_POST['nick']
$_POST['score']

If you want to refer to the items as $_POST['result'] and use your json decoding approach, youll need to package it this way:

var result = {  result : { "nick":nickName, "score":gameScore } };
share|improve this answer
1  
and there is no need to use json_decode 2 times :) – Svetlio Nov 1 '12 at 18:48
Excellent point and one that I missed. Except in very rare cases, you should never have to json_decode your input twice – user1026361 Nov 1 '12 at 18:50
okay so, with nick and score separated from post,it's working, but i get still error back on ajax, but it inserts into database. on second approach with json_decode, nothing works, even inserting failed. why? – Sándor Tamás Nov 1 '12 at 18:54
Can you edit your OP to reflect the new code? – user1026361 Nov 1 '12 at 19:52
okay, u updated it. – Sándor Tamás Nov 1 '12 at 19:59
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.