After very little success in using JQuery .post methods I wanted to try asking this here before giving up. I am serving a couple JS based games on a site using PHP as members can log into the site and through sessions/cookies remain logged and play games under their account. One game I have coded purely in PHP and it is simple to update the users database field for each game stating how many matches they've played, won, lost or tied. But one or two of the other games I am working on are in JavaScript using JQuery.

I had the game working fine in alerting the player whether they had won, lost or the match was a draw. I then attempted to add JQuery's .$post function to each case to send the result to a 'non-visible' (ie: it does not output HTML and is not seen by the player) php script which would update the appropriate field of the appropriate MySQL database.

First is the area of the game JS where the winner of the match is determined, an alert informs the player of the outcome and (will hopefully) update the database.

if (all==1){ alert("You won, congratulations!"); wn++; $.post("update_scores.php",
               outcome: "won"
if (all==2){ alert("Gotcha!  I win!"); ls++; $.post("update_scores.php",
               outcome: "loss"
if (all==3){ alert("We tied."); ts++; $.post("update_scores.php",
               outcome: "tied"

And next is the update_scores.php contents. I am attempting to capture the value of 'outcome' so that the script updates the proper field in the user's database row.

$outcome = $_POST['outcome'];

if($outcome == "won") {
    mysql_query("UPDATE `playerdb` SET `won`= `won` + 1 WHERE `user` = '$username'");

if($outcome == "lost") {
    mysql_query("UPDATE `playerdb` SET `lost`= `lost` + 1 WHERE `user` = '$username'");

if($outcome == "tied") {
    mysql_query("UPDATE `playerdb` SET `ties`= `ties` + 1 WHERE `user` = '$username'");

It is important to note that both PHP pages (the game page and score update page) uses a common function to check for an active session and fills the username variable from there. Also not in the update_score.php code are the database connection (but it does connect correctly). I created another page to send the 'outcome' as I am trying to get the JQuery to do and update_score.php handles it and updates the correct field of the correct users' row without issue. My issues seems to be in either getting JQuery to properly send the outcome info or the update_score.php's method of recieving that info.

I know that this is an incredibly specific case but after hours of scouring I am finding all too little info about JQuery post and so hopefully others could potentially gain knowledge from any possible solutions.

Thanks in advance to anyone who is able to point me towards the right direction!

3 Years
Discussion Span
Last Post by AleMonteiro

What kind of error are you getting?
Try using developer console to analyse the http request(often in Network tab).
Also, use the $.ajax method so you can get an error callback to debug it.


var msg, outcome;

if (all==1){ wn++; msg ="You won, congratulations!";  outcome = "won" }
else if (all==2){ ls++; msg ="Gotcha!  I win!";  outcome = "loss" }
else if (all==2){ ts++; msg ="We tied.";  outcome = "tied" }


    type: "POST",
    url: "update_scores.php",
    data: { outcome : outcome },
    success: function(data, textStatus) {
        console.log("Request Result: " + textStatus);
    error: function(event, jqXHR, ajaxSettings, thrownError) {
        console.log("Request Error: " + thrownError);
    dataType: dataType

And a simplification of your php script

$outcome = $_POST['outcome'];
$upate = "";

if($outcome == "won") { $update = "`won`= `won` + 1" }
else if($outcome == "lost") { $update = "`lost`= `lost` + 1"; }
else if($outcome == "tied") { $update = "`ties`= `ties` + 1"; }

mysql_query("UPDATE `playerdb` SET ". $update . " WHERE `user` = '$username'");
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.