0

With an array like mine below, how can I read the values of only the first line and the loop through the other from the second line onward? I've included comments in the code to give you an idea of what I'm trying to achieve.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP, jQuery search demo</title>
<link rel="stylesheet" type="text/css" href="my.css">
<script type="text/javascript" src="http://localhost/dev/ajax/jquery.js"></script>
<script type="text/javascript">
$(function() {

    $(".errCheck").click(function() {
             $.ajax({
                type: "GET",
                url:  "test_errors.php",
                dataType:"json",
                cache:false,

                success: function(data){
                //if 1st line of array: success == True How can I do this?
                //Redirect
                //else
                $('#errors div').empty();
                $.each(data, function (loc, msg) {//start from the second array value, in this case errOne
                $(loc).html(msg);
                });              

              },

            });    

        return false;
    });
});
</script>

</head>
<body>
<div id="container">
<div id="errors">
<div id="errOne"></div>
<div id="errTwo"></div>
<div id="errThree"></div>
</div> 
<input type="submit" class="errCheck" value="Check">
</div>
</body>
</html>

My test error messages file:

<?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);

?>
2
  • Do you have a sample of the returned json? Commented Aug 19, 2012 at 15:24
  • {"success":"false","#errOne":"Enter a valid username","#errTwo":"Enter a valid email","#errThree":"Enter a valid password"} Commented Aug 19, 2012 at 15:28

3 Answers 3

1

You can use shift() to return the first element in an array and remove it from the array

var error = data.shift();

So, var error will be your check and data can then be used in you $.each()

@Joseph Silber - Congratulations on spotting my deliberate mistake above ;-P

Use

delete myObject.property; // or myObject[property]

As per How do I remove a property from a JavaScript object?

EDIT I guess this is now the same as another answer but I felt like I might as well write the code in...

And use a proper boolean as stated elsewhere

success: function(data) {
  if(data.success == false) {
    delete data.success;
    $.each(data,function(loc, msg){
      $(loc).html(msg);
    })
  } else {
    // passed validation code
  }
}

Although I'd prefer to arrange the Json like this

{
  success: false,
  errors: [
    {fieldKey: 'string'},
    {fieldKey: 'string'}
  ]
}

Etc.

Sign up to request clarification or add additional context in comments.

1 Comment

-1 .shift is a method on the Array prototype. It won't work on objects (object properties have no order).
1

Assuming the value of data.success is a string of either true or false, use this:

if ( data.success == 'true' ) {
    window.location = 'http://new_url.com';
    return;
}

delete data.success;
// Now just run the loop

Ideally though, you should store a real Boolean in success. So in your PHP code you should use:

$errors['success'] = false;
// or
$errors['success'] = true;

Then, in your JavaScript, you would check it as a real Boolean:

if ( data.success ) {
    // redirect...
}

2 Comments

$errors['success'] = "false";, so this will always evaluate to true. he should use a false literal in PHP.
@Esailija - I missed that. Though I can't imagine why the OP won't use a real Boolean.
0

You could just use a counter as there is no index on the object, just the key :

$(function() {
    $(".errCheck").click(function() {
        $.ajax({
            type: "GET",
            url: "test_errors.php",
            dataType: "json",
            cache: false,
            success: function(data) {
                if (data.success) document.location.href = 'http://google.com';
                $('#errors div').empty();
                $.each(data, function(loc, msg) {
                    $(loc).html(msg);
                });
            },
        });
        return false;
    });
});​

3 Comments

This in indeterministic because the loop order is not defined for objects.
@pimvdb - Not only that, but I did'nt get the question at all ?
$.each(data, function(loc, msg) {//How can I start at [1] here and not [0] Eg: for (i=1; i<blah; i++)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.