Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Simple ajax/js array issue

    Heya,

    following example:
    myscript.php:
    PHP Code:
    <?php
    $msg 
    = array("Orange""Banana""Apple""Coconut");
    echo 
    json_encode($msg);  

    ?>
    ajax snippet:
    PHP Code:
                $.ajax({
                    
    type"POST",
                    
    url"myscript.php",
                    
    datadummyvar,
                    
    success: function(result) {
                        $(
    '#content').append(result[0]);
                        $(
    '#content').append(result[1]);
                        $(
    '#content').append(result[2]);
                        $(
    '#content').append(result[3]);
                    },
                    
    dataType"json"
                
    }); 

    will return the correct array values of:
    Orange Banana Apple Coconut

    I now want to loop inside ajax to iterate through the full array:

    PHP Code:
                $.ajax({
                    
    type"POST",
                    
    url"myscript.php",
                    
    datadummyvar,
                    
    success: function(result) {
                        $.
    each(result, function(iresult) {
                            $(
    '#content').append(result[i]);
                        });

                    },
                    
    dataType"json"
                
    }); 
    this loop works with the correct values for i, being 0, 1, 2, 3.
    So in other words: this SHOULD do 1:1 the same as above.

    I however only receive the very first letter of each array element, so instead of:
    Orange Banana Apple Coconut
    I get:
    O B A C

    I don't understand why because as mentioned it's 1:1 the same as above, yet the result is different :/

    Any hint is appreciated, thanks in advance ^^

  • #2
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts
    meh issue solved already:

    changed
    PHP Code:

                        
    $.each(result, function(iresult) {
                            $(
    '#content').append(result[i]);
                        }); 
    to:

    PHP Code:
                        $.each(result, function(i) {
                        
                            $(
    '#rsscontent').append(result[i]);
                            
                        }); 
    =)

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,302
    Thanks
    13
    Thanked 345 Times in 341 Posts
    the real problem is that you had a naming issue. both the array and the array element (inside the loop) had the same name.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    coffeecup (04-26-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •