By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

Js/Ajax function problem

100+
P: 374
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.

Expand|Select|Wrap|Line Numbers
  1. function Web_SQL_Storage_Open_And_Check() {
  2.     if (window.openDatabase) {
  3.         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
  4.         var db2 = db.transaction(function(tx) {
  5.           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)");
  6.           tx.executeSql("SELECT * FROM tbllog", [], function(tx, result) {
  7.  
  8.  
  9.                                 if (result.rows.length > 0) {
  10.  
  11.                                 alert("we need to submit")
  12.                                 for (var i = 0; i < result.rows.length; i++) {
  13.                                 //Submit values to the asp page using the below loop
  14.                                 var vID = (result.rows.item(i)['ID'])
  15.                                 var vPdetected = result.rows.item(i)['Pdetected']
  16.                                 var vDteOccur = result.rows.item(i)['DteOccur']
  17.                                 var vDeptRaisedBy = result.rows.item(i)['DeptRaisedBy']
  18.                                 var vDeptResp = result.rows.item(i)['DeptResp']
  19.                                 var vNCDescrip = result.rows.item(i)['NCDescrip']
  20.                                 var vNCCause = result.rows.item(i)['NCCause']
  21.                                 var vNCImmediateAct = result.rows.item(i)['NCImmediateAct']
  22.                                 var vNCLocation = result.rows.item(i)['NCLocation']
  23.                                 var vPNumOrRef = result.rows.item(i)['PNumOrRef']
  24.                                 var vEventCat = result.rows.item(i)['EventCat']
  25.                                 var vReportedEmailAddy = result.rows.item(i)['ReportedEmailAddy']
  26.                                 var vLocation_Category = result.rows.item(i)['Location_Category']
  27.  
  28.                                 var request = $.ajax({
  29.                                 url: "untitled.asp",
  30.                                 type: "post",
  31.                                 data: {
  32.                                 DteOccur: vDteOccur,
  33.                                 Pdetected: vPdetected,
  34.                                 DeptRaisedBy: vDeptRaisedBy,
  35.                                 DeptResp: vDeptResp,
  36.                                 NCDescrip: vNCDescrip,
  37.                                 NCCause: vNCCause,
  38.                                 NCImmediateAct: vNCImmediateAct,
  39.                                 NCLocation: vNCLocation,
  40.                                 PNumOrRef: vPNumOrRef,
  41.                                 EventCat: vEventCat,
  42.                                 ReportedEmailAddy: vReportedEmailAddy,
  43.                                 Location_Category: vLocation_Category
  44.                                 },
  45.                                 dataType: "html"
  46.  
  47.                                 }
  48.  
  49.                                 );
  50.  
  51.                                 request.done(function() { alert("done"); })
  52.                                 request.fail(function() { alert("error"); })
  53.                                 request.always(function() { alert("complete"); });
  54.  
  55.  
  56.                                 }
  57.  
  58.  
  59.  
  60.                                 } 
  61.                                 else {
  62.                                 //no records found so do nothing
  63.                                 alert("do nothing")
  64.                                 } //end of if statement
  65.  
  66.             }, function(tx, error) {
  67.                 alert("error retrieving")
  68.             })
  69.  
  70.  
  71.  
  72.  
  73.           //tx.executeSql();
  74.  
  75.  
  76.         });
  77.     }
  78.  
  79. }
Jan 30 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.