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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With