1

my query gives result query_string and query_string_id i want that result to provide data for ajax and send it to php

but i dont know how to send associative array through ajax

pls help me to build my code

function querySuccessEnds(tx,results) {
    var len = results.rows.length;
    console.log("DEMO table: " + len + " rows found.");
    var deltaArray=new Array();
    for (var i=0; i<len; i++){
        deltaArray[i]=[];
        deltaArray[i]['query']=results.rows.item(i).query_string
        deltaArray[i]['sync_query_id']=results.rows.item(i).sync_query_id
    }

    var data_to_send = JSON.stringify(deltaArray);
    console.log("data"+data_to_send);
    $.ajax({//to get online data
        type:"POST",
        url:galileoServer + "actions.php",
        data:"get=update&queries="+data_to_send,
        success:function(result){
            console.log(result);
        },
        error: function(xhr, status, error) {
            console.log(xhr.responseText);
        }
    }); //EOC ajax

}// EOC successUpdate

my php file

$data = json_decode(stripslashes($_REQUEST['queries']));


foreach($data as $a){
    echo $a->sync_query_id;
    echo $a->query;
}
5
  • i think you meant to use JSON.stringify(deltaArray); Commented Feb 27, 2015 at 12:14
  • i used it but its of no use Commented Feb 27, 2015 at 12:14
  • what exactly does the script receive? maybe there are breaking characters in the query....try using encodeURIComponent(data_to_send); Commented Feb 27, 2015 at 12:18
  • i cannot see data at this line means data_to_send hasn't stringyfy console.log("data"+data_to_send); Commented Feb 27, 2015 at 12:20
  • try without json stringify: data:{get:'update',queries:deltaArray} Commented Feb 27, 2015 at 12:22

1 Answer 1

2

Depending on the volume of the data sent, i'd suggest either using the data directly:

$.ajax({
    type:"POST",
    url:galileoServer + "actions.php",
    data:{get:'update',queries:deltaArray},
    success:function(result){
        console.log(result);
    }
});

or using FormData:

var formdata = new FormData();
formdata.append("get","update");
formdata.append("queries",deltaArray)

$.ajax({
    type:"POST",
    url:galileoServer + "actions.php",
    data:formdata,
    success:function(result){
        console.log(result);
    }
});

ofc this leads to a change in your php:

$data = $_REQUEST["queries"];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.