繁体   English   中英

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

[英]How do I Add/combine my ajax call output to mysql insert query?

我试图使用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);
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM