0

I have a little app, that uses WebSQL for data's storage.
I want to sync this data with web-server (PHP+MySQL)
My main problem that I have no idea how can I create a JSON from WebSQL to transfer it.

//view the result from DB on a web-page
    $('body').html('<ul id="dataAllHere"></ul>')
     mybase.init.getAll = function(){
           var database = mybase.init.db;
           database.transaction(function(tx){
                  tx.executeSql("SELECT * FROM table", [], function(tx,result){
                         for (var i=0; i < result.rows.length; i++) {
                                item = result.rows.item(i).item;
                                due_date = result.rows.item(i).due_date;
                                the_type = result.rows.item(i).the_type;
                                id = result.rows.item(i).ID;
                                showAll(item,due_date, the_type, id);
                         }
                  });
           });
    }

    function showAll(item,due_date, the_type, id){
          $('#dataAllHere').append('<li>'+item+' '+due_date+' '+the_type+' '+id+'</li>');
    }
    mybase.init.getAll();

I'm not really familiar with JSON and I'll be happy about any help and advice.

2 Answers 2

0

Basically you create an object/array and encode it to json, which looks the same, but string formatted. For your code:

var myJson = [];
for (var i=0; i < result.rows.length; i++) {
    item = result.rows.item(i).item;
    due_date = result.rows.item(i).due_date;
    the_type = result.rows.item(i).the_type;
    id = result.rows.item(i).ID;
    showAll(item,due_date, the_type, id);

    myJson.push({item: item, due_date: due_date, the_type: the_type, id: id});
}

$.ajax({
    method: 'post',
    data: myJson,
    type: 'json',
    url: 'target.php'
})
1
  • TYWM! Strange, but it tells me that myJson is undefined.
    – sergey_c
    Jun 23, 2013 at 19:13
-1

you can simplify the loop if you need to just push every thing from the result to json.

for (var i=0; i < result.rows.length; i++) {
    myJson.push(result.rows.item(i));
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.