我想从一个数据库检索数据使用ajax调用,然后将数据放入插入查询,以便它可以添加到新的数据库。 (PHP的MySQL到phonegap本地数据库)。这是我需要结合的两个代码。 ajax代码当前只是输出到一个表中,我只是为了看到它正在工作。 javascript函数可以工作并添加到数据库中。如何添加/组合我的ajax调用输出到mysql插入查询?
jQuery(document).ready(function(){
jQuery.ajax({
url : "http://cmlsys/toby/fetchdata.php",
type : "POST",
dataType : "json",
data : "param=no",
success : function (html){
jQuery.each(html, function(key, value){
$("table#DOM").append('<tr><td>'+value.CurrencyCode+'</td></tr>');
});
}, error : function (e){
alert(e);
}
});
});
function getEmployees(tx) {
var sql = "select id, CurrencyCode " + "from employee";
tx.executeSql(sql, [], getEmployees_success);
}
function populateDB(tx) {
$('#busy').show();
tx.executeSql('DROP TABLE IF EXISTS employee');
var sql =
"CREATE TABLE IF NOT EXISTS employee ("+
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"CurrencyCode VARCHAR(50))";
tx.executeSql(sql);
tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES (1,'**THE AJAX RETURN**')");
}
这里是我的所有代码 -
var db;
var dbCreated = false;
var scroll = new iScroll('wrapper', { vScrollbar: false, hScrollbar:false, hScroll: false });
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
db = window.openDatabase("EmployeeDirectoryDB", "1.0", "PhoneGap Demo", 200000);
if (dbCreated)
\t db.transaction(getEmployees, transaction_error);
else
\t db.transaction(populateDB, transaction_error, populateDB_success);
}
function transaction_error(tx, error) {
\t $('#busy').hide();
alert("Database Error: " + error);
}
function populateDB_success() {
\t dbCreated = true;
db.transaction(getEmployees, transaction_error);
}
function getEmployees(tx) {
\t var sql = "select id, CurrencyCode " + "from employee";
\t tx.executeSql(sql, [], getEmployees_success);
}
function getEmployees_success(tx, results) {
\t
\t $('#busy').hide();
var len = results.rows.length;
for (var i=0; i<len; i++) {
\t var employee = results.rows.item(i);
\t \t $('#employeeList').append('<p class="line2">' + employee.CurrencyCode + '</p>');
}
\t
\t setTimeout(function(){
\t \t scroll.refresh();
\t },100);
\t db = null;
}
jQuery(document).ready(function() {
jQuery.ajax({
url: "http://cmlsys/toby/fetchdata.php",
type: "POST",
dataType: "json",
data: "param=no",
success: function (html) {
populateDB(tx, html);
getEmployees(tx);
},
error: function (e) {
alert(e);
}
});
});
function populateDB(tx, html) {
$('#busy').show();
tx.executeSql('DROP TABLE IF EXISTS employee');
var sql =
"CREATE TABLE IF NOT EXISTS employee (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"CurrencyCode VARCHAR(50))";
tx.executeSql(sql);
var values = jQuery.map(html, function (val, i) {
return "('" + val.CurrencyCode + "')";
}).join(',');
tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES " + values);
}
\t
\t
请帮助我的人所有的努力都失败了:( – rapidwaters