Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

How can I pass variables in loop to a PHP file using AJAX?

var lat;
var lng;
var array = [ 22.399602, 114.041176, 22.344043, 114.0168, 22.327529, 114.087181 ];
console.log(array);
for (var i = 0; i < 6; i += 2) {
    lat = array[i];
    console.log("lat" + i + "=" + lat);
    for(var j = i + 1; j <= i + 1; j++) {
        lng = array[j];
        console.log("lng" + j + "=" + lng);
    }
}

My array is here:

enter image description here

In this case I want pass each lat and long to my database. My database will looks like this:

username: Neo
lat=22.399602
long=114.041176
lat=22.344043
long=114.0168
lat=22.327529
long=114.087181

I also test this AJAX function but it isn't working for an array.

$.post('send.php', { latitude: lat, longitude: lng }, function(data) {
    $('#result').html(data);
});

What should I do now?

share|improve this question
    
for(var j=i+1; j<=i+1;j++) what's this for – Ramanlfc Nov 16 '15 at 14:17
    
How do you populate the array initially? – RamRaider Nov 16 '15 at 14:19
    
it's get lat, long from array. Array included both lat and long. 1st loop get lat, and second loop is get only longitude. – Neo Teo Nov 16 '15 at 14:20
    
what error did $.post() give you? – Ramanlfc Nov 16 '15 at 14:25
    
i didn't use array send to php file using $.post() how to pass it – Neo Teo Nov 16 '15 at 14:30
up vote 0 down vote accepted

if you are certain that array will contain latitude and longitude one after the other then you don't need a nested loop.

var lat;
var lng;
var array = [22.399602,114.041176 , 22.344043,114.0168 , 22.327529,114.087181];
//console.log(array);
for(var i=0;i<array.length;i+=2)
{
    lat=array[i];
    long = array[i+1];
    console.log(lat+"    "+long);
    $.post('send.php', {latitude:lat, longitude:long}, 
    function(data)
    {
        $('#result').html(data);
    });
}
share|improve this answer
    
Thanks you man! It's very help me. – Neo Teo Nov 16 '15 at 15:17

If you were to structure your initial array in a more logical manner the problem would be much simpler for you. Consider:

function sendlatlng(lat,lng){
    var http=new XMLHttpRequest();
    http.onreadystatechange=function(){
        if( http.readyState==4 && http.status==200 ) console.info( http.response );
    };
    var headers={
        'Content-type': 'application/x-www-form-urlencoded'
    };
    http.open('POST', '/send.php', true );
    for( header in headers ) http.setRequestHeader( header, headers[ header ] );
    http.send( 'lat='+lat+'&lng='+lng );
}



var array = [ [22.399602, 114.041176], [22.344043, 114.0168], [22.327529, 114.087181] ];

for( i=0; i < array.length; i++ ){
    lat=array[ i ][ 0 ];
    lng=array[ i ][ 1 ]
    console.log( 'lat: %s lng: %s',lat,lng );
    sendlatlng.call( this, lat, lng );
}
share|improve this answer
    
Thanks man for helping. – Neo Teo Nov 16 '15 at 15:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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