2

How do I iterate thru an array like this using 'for' in jquery. This one has me totally lost. I cant figure how to get those values out from an array like this and I cant change the way the array is done.

//Php
$errors['success']   = false;
$errors['#errOne']   = "Enter a valid username";
$errors['#errTwo']   = "Enter a valid email";
$errors['#errThree'] = "Enter a valid password";
echo json_encode($errors);//

dataType:"json",
cache:false,
success: function(data){
for (i=1; i<?; i++){//Start at 1
//I'm totally lost here.
//Output: "#errOne" "Enter a valid username" ->Loop thru remaining messages
}   
},
Norman
  • 6,159
  • 23
  • 88
  • 141

4 Answers4

2

since your passing data as parameter, you can access the data

try something like this :

var $errors = {};
$errors['success']   = false;
$errors['#errOne']   = "Enter a valid username";
$errors['#errTwo']   = "Enter a valid email";
$errors['#errThree'] = "Enter a valid password";
    data : $errors;
    success: function(data){
    for (var i in data ){
       console.log(i + ':' + data[i]);
    }
Shreedhar
  • 5,502
  • 3
  • 22
  • 27
  • @some i am creating object in the first line itself. and wher do you think i am abusing arrays in javascript? – Shreedhar Aug 20 '12 at 06:31
  • This is 99% what I needed. Only thing is: any idea how to start this at [1] and not [0]? – Norman Aug 20 '12 at 06:39
2

May be you need this:

success: function(data){
    $.each(data, function(key, value) { 
        alert(key + ': ' + value); 
    });
}
Kir
  • 694
  • 4
  • 7
  • I started off which $.each. Would be really nice if we could tell $.each where to start- at [0] or [1]. Eg: for (i=1;... – Norman Aug 20 '12 at 06:40
  • 1
    In different browsers sorting of Data will be different. In FF it will be as is, but Chrome will sort Data by keys automatically. So in different browsers first element will be different – Kir Aug 20 '12 at 07:04
0
for (var key in data){
   alert(key); // gives keys  success, #errOne, #errTwo, #errThree
   alert(data[key]); // gives values  false, Enter a valid username, Enter a valid email, Enter a valid password
}  
Diode
  • 24,570
  • 8
  • 40
  • 51
  • when using for (var key in data) can we tell it to start at [1] and not [0]? Eg: for (i=1;... – Norman Aug 20 '12 at 06:42
0

If your PHP code is like this:

// Use $errors = array(...) if your PHP version is < 5.4
$errors = [
    'success' => false, // Why is 'success' part of the $errors array?
    'error_1' => 'Enter a valid username.',
    'error_2' => 'Enter a valid email.',
    'error_3' => 'Enter a valid password.'
];

print json_encode($errors);

, then in your JavaScript, you can do the following:

$.ajax({
    'url': 'your.url.com/data.json',
    'dataType': 'json',
    'cache': false, // Why are you doing this?
    'success': function (json_data) {
        var i = json_data.length;
        while (i--) {
            alert(json_data[i]); // Or whatever you want to do with the data.
        }
    }
});

I'm pretty sure this is one of the best ways to do this. Someone please correct me if I'm wrong.

jonathanmarvens
  • 402
  • 1
  • 3
  • 10