I have a small web app built in PHP and Javascript/jQuery.
The app involves a random number generator and when it lands on say number 4, the user wins.
However the prize is pulled from a database and after 5 prizes a day are won. the user is unable to win.
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.
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.
I have a javascript function OnStart. So if i could perform the SQL query here it would happen everytime the user throws the die.
How can I run a php statement from a jquery function?
If it helps here is the statement:
$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'];
}
Thanks
EDIT:
So could I possibly use something like this with the php in a seperate file.
<script type="text/javascript">
function performAjaxSubmission() {
$.ajax({
url: 'file.php',
method: 'POST',
data: {
action: 'save',
arg1: 'val1',
arg2: 'val2'
},
success: function() {
alert("success!");
}
});
return false; // <--- important, prevents the link's href (hash in this example) from executing.
}
jQuery(document).ready(function() {
$("#linkToClick").click(performAjaxSubmission);
});
</script>
Save the posted code in a php file, let’s call it
prizes_available.php. This file should also output whether prizes are available or not, so create the result object and assign the value to it:If you call this script in browser, it will output something like this:
Using jQuery, you can easily convert this string into an object again: