gpt4 book ai didi

javascript - 如何将我的 ajax 调用输出添加/合并到 mysql 插入查询?

转载 作者:搜寻专家 更新时间:2023-10-30 23:43:00 25 4
gpt4 key购买 nike

我正在尝试使用 ajax 调用从一个数据库中检索数据,然后将数据放入插入查询中,以便将其添加到新数据库中。 (php mysql 到 phonegap 本地数据库)。这是我需要合并的两个代码。 ajax 代码目前只是输出到一个表中,我这样做只是为了查看它是否正常工作。 javascript 函数确实有效并添加到数据库中。

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)
db.transaction(getEmployees, transaction_error);
else
db.transaction(populateDB, transaction_error, populateDB_success);
}

function transaction_error(tx, error) {
$('#busy').hide();
alert("Database Error: " + error);
}

function populateDB_success() {
dbCreated = true;
db.transaction(getEmployees, transaction_error);
}


function getEmployees(tx) {
var sql = "select id, CurrencyCode " + "from employee";
tx.executeSql(sql, [], getEmployees_success);
}


function getEmployees_success(tx, results) {

$('#busy').hide();
var len = results.rows.length;
for (var i=0; i<len; i++) {
var employee = results.rows.item(i);

$('#employeeList').append('<p class="line2">' + employee.CurrencyCode + '</p>');
}

setTimeout(function(){
scroll.refresh();
},100);
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);
}


最佳答案

您可以将 ajax 成功数据传递给成功闭包中的 populateDB 函数。请检查下面的代码。我猜你的 tx 是一个全局变量。

    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) db.transaction(getEmployees, transaction_error);
else CallAjax();
}

function transaction_error(tx, error) {
$('#busy').hide();
alert("Database Error: " + error);
}

function populateDB_success() {
dbCreated = true;
db.transaction(getEmployees, transaction_error);
}


function getEmployees(tx) {
var sql = "select id, CurrencyCode from employee";
tx.executeSql(sql, [], getEmployees_success);
}


function getEmployees_success(tx, results) {

$('#busy').hide();
var len = results.rows.length;
for (var i = 0; i < len; i++) {
var employee = results.rows.item(i);

$('#employeeList').append('<p class="line2">' + employee.CurrencyCode + '</p>');
}

setTimeout(function () {
scroll.refresh();
}, 100);
db = null;
}


function CallAjax() {
jQuery.ajax({
url: "http://cmlsys/toby/fetchdata.php",
type: "POST",
dataType: "json",
data: "param=no",
success: function (html) {
$('#busy').show();
db.transaction(function (tx) {
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);
}, transaction_error, populateDB_success);

},
error: function (e) {
alert(e);
}
});
}

关于javascript - 如何将我的 ajax 调用输出添加/合并到 mysql 插入查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com