Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I return an array of a customer's addresses with PHP via Ajax (return as addresses). I document.write the addresses.responseText which displays the array perfectly

Array ( [0] => Array ( [addressID] => 15804 [customerID] => 10 [loanID] => [address1] => 6 Road [address2] => [address3] => [town] => Their Town [county] => Their County [postCode] => Their postCode [reason] => [current] => 1 [timestamp] => 2013-03-13 14:41:39 ) [1] => Array ( [addressID] => 10 [customerID] => 10 [loanID] => [address1] => 21A Road [address2] => [address3] => [town] => Their 2nd Town [county] => Their 2nd County [postCode] => Their 2nd postCode [reason] => [current] => 0 [timestamp] => 2013-03-13 14:41:39 ) )

I'm having problem though with looping through and using the array information.

new Ajax.Request('includes/ajax.php',
        {
            method:'post',
            parameters: {addCustID: customerID},
            onSuccess: function(addresses){

            document.write(addresses.responseText);

                for (var i in addresses)
                {

                    if(i == "responseText") 
                    {                           
                        addressExpander.addEntry(addresses[i]);
                    }
                }
            },
            onFailure: function(){ alert('Something went wrong...') }
        });

I want to run addressExpander.addEntry(addresses[i]); for each address, but it only displays once, not for each address.

I've tried looping through addresses.responseText in the if(i == "responseText") loop but it doesn't do anything.

I'm using Prototype by the way, it is already being used in the page.

share|improve this question
 
change if(i == "responseText") for if(addresses[i] == "responseText") otherwise you're comparing the index instead of the actual value of the variable. –  Jeff Noel Aug 20 at 14:27
 
Thanks, though I did that and now addressExpander.addEntry(addresses[i]); doesn't run at all –  user2622416 Aug 20 at 14:36
 
That means none of the values contained in addresses hold the value "responseText". –  Jeff Noel Aug 20 at 14:53
add comment

2 Answers

up vote 0 down vote accepted

It seems like the problem you're having is that there are pretty significant differences between PHP arrays and Javascript arrays.

To help compensate for these difference, our overlords created the almighty JSON format which helps enable the sharing of inter-language data structures. Before echoing your PHP response, use json_encode like so:

echo json_encode($your_php_arr);

Your JS code should be able to work without any modifications. If it doesn't, try adding a JSON.parse within your onSuccess callback:

var your_js_arr = JSON.parse(addresses.responseText);

for (var i in your_js_arr)
{               
    addressExpander.addEntry(your_js_arr[i]);
}
share|improve this answer
 
Thanks, I get 2 address blocks as wanted which is good, but there are also 37 blank ones produced after that. –  user2622416 Aug 21 at 9:10
 
It loops 39 times instead of 2 –  user2622416 Aug 21 at 9:11
 
Wierd as my php array produces 2 addresses as in my original example –  user2622416 Aug 21 at 9:44
 
Solved it, I added 'if (your_js_arr[i]['address1']) addressExpander.addEntry(your_js_arr[i]);' –  user2622416 Aug 21 at 12:58
add comment

Assuming that this line:

document.write(addresses.responseText);

is the one that's producing the output you posted, you're printing out addresses.responseText and it's the array of addresses.

As you said in your answer, by the looks of it, you need to loop through addresses.responseText instead:

for (var i=0;i<addresses.responseText.length;i++)
{     
     addressExpander.addEntry(addresses.responseText[i]);
}
share|improve this answer
 
I did try that, it loop about 50 times! –  user2622416 Aug 21 at 9:09
 
I appreciate your help –  user2622416 Aug 21 at 9:39
add comment

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.