Skip to content Skip to sidebar Skip to footer

Select From Mysql Database On Javascript Call

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

Solution 1:

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:

$result = newstdclass();
$result->prizes_available = $prize != null;
echo json_encode($result);

If you call this script in browser, it will output something like this:

{ prizess_available:true }

Using jQuery, you can easily convert this string into an object again:

$.getJSON('prizes_available.php', function(result)  {
    if (!result.prizes_available) {
        alert('no more prizes');
    }
});

Solution 2:

You have to use AJAX, jQuery contains some easy to use functions for working with AJAX.

Usage is simple, you can read about that in docs.jquery.com

If you want pass some array to your js script, look for JSON functions ($.getJSON in jQuery, json_decode / json_encode in PHP)

Post a Comment for "Select From Mysql Database On Javascript Call"