Js/Ajax function problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • munkee
    Contributor
    • Feb 2010
    • 374

    Js/Ajax function problem

    Hi All,

    I use the following code to query my websql database to see if there are any records.

    If records are found then I use an ajax request to submit these to an asp page which in turn pushes the form information on to a sql server.

    So far everything is good however I now want to delete each of the records from the WebSql database only when the ajax request returns a success.

    I can't for the life of me get my head around javascript functions and scope correctly when they are nested this big (i've only been writing js/jquery now for a couple of weeks).

    I know I need to capture the ID which I have set as a variable vID an then execute another sql statement to delete the record, nested within the success criteria of my ajax call. I cant get vID to reference correctly and then I do not know how to set up the call to execute the sql again based on this.

    Code:
    function Web_SQL_Storage_Open_And_Check() {
        if (window.openDatabase) {
            var db = openDatabase("ER_Nonconformance", "1.0", "Non-conformance Report DB", 4 * 1024 * 1024); //creates our database if it does not exist at 4mb size
            var db2 = db.transaction(function(tx) {
              tx.executeSql("CREATE TABLE IF NOT EXISTS tbllog (ID INTEGER PRIMARY KEY ASC AUTOINCREMENT, DteOccur datetime, Pdetected nvarchar(50), DeptRaisedBy int, DeptResp int, NCDescrip nvarchar(255), NCCause nvarchar(255), NCImmediateAct nvarchar(255), NCLocation nvarchar(100), PNumOrRef nvarchar(30), EventCat int, ReportedEmailAddy nvarchar(100), Location_Category int)");
              tx.executeSql("SELECT * FROM tbllog", [], function(tx, result) {
                
               
                                    if (result.rows.length > 0) {
                                    
                                    alert("we need to submit")
                                    for (var i = 0; i < result.rows.length; i++) {
                                    //Submit values to the asp page using the below loop
                                    var vID = (result.rows.item(i)['ID'])
                                    var vPdetected = result.rows.item(i)['Pdetected']
                                    var vDteOccur = result.rows.item(i)['DteOccur']
                                    var vDeptRaisedBy = result.rows.item(i)['DeptRaisedBy']
                                    var vDeptResp = result.rows.item(i)['DeptResp']
                                    var vNCDescrip = result.rows.item(i)['NCDescrip']
                                    var vNCCause = result.rows.item(i)['NCCause']
                                    var vNCImmediateAct = result.rows.item(i)['NCImmediateAct']
                                    var vNCLocation = result.rows.item(i)['NCLocation']
                                    var vPNumOrRef = result.rows.item(i)['PNumOrRef']
                                    var vEventCat = result.rows.item(i)['EventCat']
                                    var vReportedEmailAddy = result.rows.item(i)['ReportedEmailAddy']
                                    var vLocation_Category = result.rows.item(i)['Location_Category']
                                    
                                    var request = $.ajax({
                                    url: "untitled.asp",
                                    type: "post",
                                    data: {
                                    DteOccur: vDteOccur,
                                    Pdetected: vPdetected,
                                    DeptRaisedBy: vDeptRaisedBy,
                                    DeptResp: vDeptResp,
                                    NCDescrip: vNCDescrip,
                                    NCCause: vNCCause,
                                    NCImmediateAct: vNCImmediateAct,
                                    NCLocation: vNCLocation,
                                    PNumOrRef: vPNumOrRef,
                                    EventCat: vEventCat,
                                    ReportedEmailAddy: vReportedEmailAddy,
                                    Location_Category: vLocation_Category
                                    },
                                    dataType: "html"
                                    
                                    }
                                    
                                    );
                                    
                                    request.done(function() { alert("done"); })
                                    request.fail(function() { alert("error"); })
                                    request.always(function() { alert("complete"); });
    
                                   
                                    }
                                    
                                      
                                      
                                    } 
                                    else {
                                    //no records found so do nothing
                                    alert("do nothing")
                                    } //end of if statement
                
                }, function(tx, error) {
                    alert("error retrieving")
                })
              
              
              
              
              //tx.executeSql();
              
              
            });
        }
     
    }
Working...