0
$.ajax({
    url: 'http://localhost/xampp/htdocs/ajax.php',

    contentType: 'application/json',
    type: 'post',
    data: {

//score is a new variable and count2 is an integer variable which i want to send it to the database "score":"count2" },

    success: function () {
        alert("ok");

    },
    error: function () {
       alert("error");
    }
});    

The above code alerts me "ok".

function connect(){
    $connect = mysql_connect('localhost','root','') or die("ERROR");
    $connect2Database = mysql_select_db('fypdb', $connect);
    return $connect;
}

if(isset($_POST)){

    if($connect = connect()){

        $query = "INSERT INTO fypdbtable (score) VALUES ('".$_POST['score']."');";
        $completeQuery = mysql_query($query, $connect);
    }
}

I think the problem is on "insert into" query because in ajax.php prompts me Notice: Undefined index: score in C:\xampp\htdocs\xampp\htdocs\ajax.php on line 14 where it is the query line

Any help would be appreciate

  • 1
    You **need** to stop using the `mysql_` extension. Use MySQLi or PDO instead. Also, your code is wide open to SQL injection, read [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/q/60174/3899908) – worldofjr Nov 22 '14 at 21:35
  • 1
    Where you have `if(isset($_POST)){` use `if(isset($_POST['score'])){` instead. I very much suspect that this value is not set. – worldofjr Nov 22 '14 at 21:39
  • It is set as count2 from fyp.php and am trying to move it in ajax.php and from there to my database – Leandros Ioannou Nov 22 '14 at 22:46

2 Answers2

1

Problem here is in your ajax code. You are using contentType: 'application/json' but you try to acces param in your php script with $_POST, but post works with form encoded content types. So you can remove contentType from your ajax call and it will use default which is
contentType:'application/x-www-form-urlencoded; charset=UTF-8'

EDIT Make your ajax code like that:

$.ajax({
url: 'http://localhost/xampp/htdocs/ajax.php',
type: 'post',
data: {
    "score":"count2"
},

success: function () {
    alert("ok");

},
error: function () {
   alert("error");
}
});
alphawow
  • 350
  • 2
  • 7
  • Thank for your effort but still does not pass the variable to ajax.php – Leandros Ioannou Nov 22 '14 at 22:07
  • As I can see in his php script he does not handle this like json, but like normal post, so this is why his "insert into" is not working. – alphawow Nov 22 '14 at 22:08
  • You might be right on that. Its been a while since I used jquery ajax. Edit: Yes you are. I was confusing contentType and datatype. From the looks of it, you shouldn't be setting the contentType for this. – Devon Nov 22 '14 at 22:09
  • Yes, basically i want to send the javascript variable to the player that is logged in, am trying for that more than 1 year, what is the problem??? – Leandros Ioannou Nov 22 '14 at 22:18
  • Notice: Undefined index: score in C:\xampp\htdocs\xampp\htdocs\ajax.php on line 19 **and** function connect() does **not** work, i added some echo inside and if statement outside but nothing – Leandros Ioannou Nov 22 '14 at 22:23
  • I've explained why it is not working in my answer. Copy my code and try, it must work. You get this notice because content is posted as json and `$_POST['score']` is not set... – alphawow Nov 22 '14 at 22:28
  • The same s**t... Anyway thanks a lot for your effort PS: I added a comment line may is there the problem – Leandros Ioannou Nov 22 '14 at 22:32
0

The "error" message tells you, that $_POST['score'] is not defined. But thats just a warning. If you want to know if your variable is set, use isset() function or turn off warnings in the configuration.

harrow
  • 118
  • 8
  • It's actually a notice, not a warning, but it is important to keep those on during development. – Devon Nov 22 '14 at 22:13
  • Yes, but in productive enviroment you should turn it of – harrow Nov 22 '14 at 22:15
  • Yes, production environment should not have anything reported, but should have errors logged. You also shouldn't be using `$_POST` variables in a query in production. – Devon Nov 22 '14 at 22:17