I'm trying to get the contact list from the phone and send it through ajax and php to mysql. This is the code I'm using:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var options = new ContactFindOptions();
options.filter=""; // empty search string returns all contacts
options.multiple=true; // return multiple results
filter = ["*"];
navigator.contacts.find(filter, onSuccess, onError, options);
}
function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++){
var phoneNumbers = contacts[i].phoneNumbers;
//alert(JSON.stringify(phoneNumbers));
for (var j=0;j<phoneNumbers.length; j++){
//if(phoneNumbers[j].type == "mobile"){
//alert(phoneNumbers[j].value);
var jsonString = JSON.stringify(phoneNumbers[j].value);
//var dataString ="title="+jsonString+"&insert=";
var dataString = JSON.parse(jsonString);
//$("#contactList").append("<li><input type='text' name='box["+i+"]' id='box' value='"+dataString+"'></li>");
$("#contactList").append("<input type='text' name='name[]' id='name' value='"+phoneNumbers[j].value+"'>");
//$("#contactList").listview("refresh");
//}
}
}
}
function onError(contactError) {
//alert('onError!');
}
AJAX
var fields = $('#myform').serialize();
var jsonString = JSON.stringify(fields);
console.log(fields);
$.ajax(
{
url : "retorno.php",
type: "POST",
dataType: "json",
data: {data : jsonString},
cache: false,
success:function(data, textStatus, jqXHR)
{
$('#stage2').html(data);
}
});
});
PHP
$name = $_POST['data'];
If anyone can help, I'll be very grateful.