Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can i use PhoneGap Jquery to make ajax calls?

Can I use jQuery AJAX calls in PhoneGap to run a PHP file that gets records from a database, or should I use JavaScript AAJAX?

like image 240
Kalaji Avatar asked Aug 12 '11 22:08

Kalaji


1 Answers

You certainly can use jQuery Ajax functions in your PhoneGap applications. Here is a demo:

-- JavaScript in App --

$('#some_page_id').bind('pageshow', function () {
    $.get('http://domain.com/path/to/script.php?get_param=value', function (data) {
        $(this).find('div[data-role="content"]').append(data);
    });
});

-- PHP on Server --

if (isset($_GET['get_param']) && $_GET['get_param'] == 'value') {
    $query = mysql_query("SELECT * FROM some_table WHERE some_col='something'", $db_handle);
    if (mysql_affected_rows() > 0) {
        while ($row = mysql_fetch_assoc($query)) {
            echo "<div>" . $row['some_other_col'] . "</div>";
        }
    } else {
        echo "No Data Found";
    }
}

The above example will query the PHP script on the server every time the '#some_page_id' page is shown and append the data grabbed to the <div data-role="content"> tag. You can also use .html(data) instead of .append(data) to replace the HTML rather than add to it.

UPDATE

I found this in the jQuery Mobile documentation which gives some excellent information about making $.ajax() calls in PhoneGap apps: http://jquerymobile.com/demos/1.0/docs/pages/phonegap.html

like image 99
Jasper Avatar answered Sep 19 '22 01:09

Jasper