title: Updating a MySql database using PHP via an onClick javascript function
body: <p>I am creating a web game for learning new words aimed at children.</p>
  
  <p>I have a set of four links each displaying a specific word retrieved from my database and a clue, I need to check that the word which has been selected matches the correct word for that clue.</p>
  
  <p>I know that I need to use javascript because of the onClick function and I can successfully check whether the word selected matches the correct word.  However, I then need to update a score held in the database if the word is matched correctly, therefore I would need to use php.</p>
  
  <p>From what I can gather this means I must use AJAX but I can't find a good example of anyone using AJAX onClick of a link to then update a database.</p>
  
  <p>I have attempted to do this...but its probably completely wrong as I couldn't get it to work properly:</p>
  
  <pre><code>//This is my link that I need to use in my game.php file where $newarray[0] is that answer I want to check against $newarray[$rand_keys]
  
  &lt;a onClick=originalUpdateScore('$newarray[0]','$newarray[$rand_keys]')&gt;$newarray[0]&lt;/a&gt;
  </code></pre>
  
  <p>//my attempt at ajax in a score.js file</p>
  
  <pre><code>var xmlHttp;
  
  function originalUpdateScore(obj,corr){
      xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
      alert ("Browser does not support HTTP Request");
      return;
  }
  
  if(corr == obj){
  
  var url="getscore.php";
  //url=url+"?q="+str;
  //url=url+"&amp;sid="+Math.random();
  xmlHttp.onreadystatechange=stateChanged;
  //xmlHttp.open("GET",url,true);
  xmlHttp.open(url,true);
  xmlHttp.send(null);
  
      alert('Correct');
  
  }
  else
  {
      alert('AHHHHH!!!');
  }
  
  window.location.reload(true);
  </code></pre>
  
  <p>}</p>
  
  <pre><code>function stateChanged() 
      { 
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
      { 
          document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
      } 
  }
  function GetXmlHttpObject()
  {
      var xmlHttp=null;
      try
      {
          // Firefox, Opera 8.0+, Safari
          xmlHttp=new XMLHttpRequest();
      }
      catch (e)
      {
          //Internet Explorer
          try
          {
              xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch (e)
          {
              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
      }
  return xmlHttp;
  </code></pre>
  
  <p>}</p>
  
  <pre><code>//attempting to update the database in a getscore.php file
  
  &lt;?php
  //$q=$_GET["q"];
  include("dbstuff.inc");
  $con = mysqli_connect($host, $user, $passwd, $dbname)
  or die ("Query died: connection");
  
  $sql= "UPDATE `temp` SET `tempScore`= `tempScore`+1 WHERE (temp.Username='$_SESSION[logname]')";
  
  $showsql = "SELECT `tempScore` FROM `temp` WHERE (temp.Username='$_SESSION[logname]')";
  $result = mysqli_query($con, $showsql);
  
  echo "$result";
  
  mysqli_close($con);
  ?&gt;
  </code></pre>