1
I’m trying to get the contact list from mobile phone and send 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’d be most grateful.
Is your PHP running inside a mobile phone? Or is PHP running on an external server?
– Guilherme Nascimento
It is running on a server outside. Instead of the url when I posted here I did not indicate the path.
– Carlos Magalhães
But then you passed
url : "http://site.com/retorno.php",
right?– Guilherme Nascimento
exactly. Except he can’t get the data from his phone into the comic book. But if I manually populate an array of <input type="text" name="name[]" value=""> it inserts into the good BD. Just do not enter when data comes from cell phone
– Carlos Magalhães
Emphasizing that I am testing through phoneGap Developer using a mobile phone
– Carlos Magalhães
Can pick up the HTTP request from the phone the moment it sends it?
– Guilherme Nascimento
Yes. Even, when I give Count in the array it inserts the total of 1 in the BD, when in fact I have 10 contacts. But if I manually enter 4 numbers into the inputs, it inserts the 4 numbers correctly. The problem is when the data comes from the phonenumbers[j]. value.
– Carlos Magalhães
I already understood the problem, I just want to know if you have how to debug the HTTP request, this is how we will know how the data was transmitted. If you can get REQUEST, copy it and paste it here.
– Guilherme Nascimento
There’s no way to debug it, unfortunately. Information is passing, because if I give Count in the Post it returns value 1, only that in fact I have 10 contacts. The question is: does it bring an array with all contacts? If so, how can I get it? Another thing, when returning to phoneGap in html it prints the source code that is commented on in PHP, including the closing of php code ?>
– Carlos Magalhães
William, when you can give me an answer I’ll be waiting. @Guilhermenascimento
– Carlos Magalhães