I am learning how to use phonegap now and learning is going great but I\'m stuck with global variable in phoneGap Api. Actually First I\'m getting the result from xml file and t
You don't even need it to be global for this. Define the lastId in the function but before the foreach. If that doesn't work, then making it global should really work, by moving the var lastId; line before function populateDB()
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS fruit');
tx.executeSql('DROP TABLE IF EXISTS fruit_benefit');
tx.executeSql('CREATE TABLE IF NOT EXISTS fruit (id INTEGER NOT NULL PRIMARY KEY, fname,fsname,fruit_icon,fruit_image)');
tx.executeSql('CREATE TABLE IF NOT EXISTS fruit_benefit (id INTEGER NOT NULL PRIMARY KEY, benefit, fruit_id)');
$.ajax({
type: "GET",
url: "data.xml",
dataType: "xml",
success: function(xml) {
var lastId;
$(xml).find('fruit').each(function(){
var fname = $(this).find('fname').text();
var fsname = $(this).find('fsname').text();
var fruit_icon = $(this).find('fruit_icon').text();
var fruit_image = $(this).find('fruit_image').text();
//$('').html(''+fname+'').appendTo('#lbUsers');
db.transaction(function(transaction) {
transaction.executeSql('INSERT INTO fruit (fname,fsname,fruit_icon,fruit_image) VALUES (?,?,?,?)',[fname, fsname, fruit_icon, fruit_image],function(transaction, results){
lastId = results.insertId;
},nullHandler,errorHandler);
});
alert(lastId);
});
}
});
}