title: How to use AJAX to store a javascript variable to my sql database
body: <p>I'm trying to save a logged in user's score to my sql database. I programmed a simple game of pong in HTML5 and javascript. Basically I need to get the score variable from the pong.js file and write that score to the database when the user hits a button on my page. I just simply can't get it to work. I'll hit the button as a logged in user and my games score will update to 1 (which is just a test) but i won't get any type of feedback from my server's php file. </p>
  
  <p>Here's my Index.php file: </p>
  
  <pre><code>    &lt;?php 
      session_start(); 
      $_SESSION['score'] = 0;
      ?&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta name = "description" content = "Josh Siegl's home web server"/&gt;
      &lt;meta name = "keywords" content = "home, web, server, pong, html5, game"/&gt;
      &lt;link rel="stylesheet" type = "text/css" href = "mycss.css" /&gt; 
      &lt;title&gt; JOSH SIEGL HOME WEB SERVER &lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;div align = "center"&gt; 
      &lt;?php if(!isset($_COOKIE['ID_SERVER']))
      {
      echo '&lt;form id = "signIn" action = "getuser.php" method = "post"&gt;
      UserName: &lt;input type = "text" name = "username"  /&gt;
      Password: &lt;input type = "password" name = "password" /&gt;
      &lt;input type = "submit" name = "SIGNIN" value = "SIGN IN"/&gt;  or    
      &lt;a href = "register.html" id = "register" style="text-decoration:none"/&gt; &lt;i&gt;       Register &lt;/i&gt;&lt;/a&gt;
      &lt;/form&gt;';
      }
      else 
      {
  
      ?&gt;
  
      &lt;p&gt; &lt;?php echo "WELCOME " . $_COOKIE['ID_SERVER'];} ?&gt; &lt;?php         if(isset($_COOKIE['ID_SERVER'])){echo '&lt;a href = "logout.php"&gt;  logout&lt;/a&gt;';} ?&gt; &lt;/p&gt;
  
      &lt;h1 id = "GAMENAME"&gt; PONG &lt;/h1&gt;
      &lt;canvas id="myCanvas" width="800" height="600"&gt;&lt;/canvas&gt;
      &lt;script type="text/javascript" src = "pong.js" id = "gamescript"&gt;&lt;/script&gt;
      &lt;div id = "status"&gt;&lt;/div&gt;
      &lt;script language = "JavaScript" type = "text/javascript"&gt;
      function Ajax_Call()
      {
  playerscore = 1;
  var hr = new XmlHttpRequest(); 
  
  var url = "submitscore.php";
  var keys = "score=" + playerscore; 
  
  hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  hr.onreadystatechange = function()
  {
      if (hr.readyState == 4 &amp;&amp; hr.status == 200)
      {
          var returndata = hr.responseText;
          document.getElementById("status").innerHTML = returndata;
      }
  }
  hr.open("GET", "submitscore.php?score="+playerscore, true);
  hr.send();
  document.getElementById("status").innerHTML = "posting..."; 
  
      }
      &lt;/script&gt; 
      &lt;input type = "submit" name = "submitscore" value = "POST SCORE" onclick =    "Ajax_Call();"&gt;&lt;/input&gt;
  
      &lt;br /&gt; &lt;p&gt; This is a pong clone, created by Josh Siegl, hosted on a home web server. 
         This game was created in HTML5 and should run on most up to date browsers. &lt;/p&gt;
      &lt;br /&gt; &lt;p&gt; I programmed this entire game in javascript and HTML5. The source code is publicly available, right click and hit inspect element &lt;/p&gt;
  
      &lt;/div&gt;     
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>the submitscore button calls Ajax_Call(), I know that this works because I updated my score variable to show that Ajax_Call is at least executing (the playerscore variable is contained within pong.js, if you need to see that file i'll post it, although I don't believe i'll need to).</p>
  
  <p>Here's my submitscore php file:</p>
  
  <pre><code>    &lt;?php
      $q=$_GET['score'];
      $con = mysql_connect("localhost", "root", "");
      if (!$con)
      {
  die(mysql_error());
      }
  
      $mydb = mysql_select_db('users', $con);
      if (!$mydb) {
      die ('Can\'t use $con : ' . mysql_error());
  }
  
  
  
      $sqlstatement = "UPDATE people
              SET score= $q WHERE username =$_COOKIE['ID_SERVER']";
  
  if (mysql_query($sqlstatement, $con))
  {
      echo "your score of: $q is now in the database";
  }
  else
  {
  die(mysql_error()); 
  }
  
      mysql_close($con);  
  
      ?&gt;
  </code></pre>
  
  <p>(this is just for practice, so don't go trying to hack my database ;) ) anyway i've been trying a bunch of different things so that's why the code may look a little bit sloppy, again all I want to do is submit my score variable to the database when the user hits the button. (I'm using firefox to test my site, so at this point to keep things simple, I didn't throw any checks for other browsers when creating my new xmlhttprequest object if you believe this is the problem let me know) </p>
  
  <p>If you could possibly tell me a direction to go in, or something to try, or at least a website that could help me out. it would be VERY much appreciated. This is my first post so if I did something wrong please let me know. Thank you very much for the help</p>