title: SELECT from mysql database on javascript call
body: <p>I have a small web app built in PHP and Javascript/jQuery. </p>
  
  <p>The app involves a random number generator and when it lands on say number 4, the user wins.</p>
  
  <p>However the prize is pulled from a database and after 5 prizes a day are won. the user is unable to win.</p>
  
  <p>Once a user wins, the prize is updated as 'queued' until they have filled out a form and claimed the prize. It is then taken out of available prizes. </p>
  
  <p>This all works fine if the user refreshes the app. However if a user begins to play the app and another user wins then the app wont update unless its refreshed as PHP is server side.</p>
  
  <p>I have a javascript function OnStart. So if i could perform the SQL query here it would happen everytime the user throws the die. </p>
  
  <p>How can I run a php statement from a jquery function?</p>
  
  <p>If it helps here is the statement:</p>
  
  <pre><code>$result = mysql_query("SELECT * FROM prizes WHERE dateavailable='$todayDate' AND queue='0' AND won='0' ORDER BY id ASC LIMIT 1");
  
  while($row = mysql_fetch_array($result))
  
  {   
      $prize = $row['prize'];
  }
  </code></pre>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <p>So could I possibly use something like this with the php in a seperate file.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    function performAjaxSubmission() {
      $.ajax({
        url: 'file.php',
        method: 'POST',
        data: {
          action: 'save',
          arg1: 'val1',
          arg2: 'val2'
        },
        success: function() {
          alert("success!");
        }
      });
      return false; // &lt;--- important, prevents the link's href (hash in this example) from executing.
    }
  
    jQuery(document).ready(function() {
      $("#linkToClick").click(performAjaxSubmission);
    });
  &lt;/script&gt;
  </code></pre>