0

I'm working on a method in my class that outputs error messages for both ajax and regular post request. The PHP part works fine, but the json part doesn't seem to. Here is my method:

public $formError = false;
public $phpErrors = '';
public $jsonErrors = array();

// ------------------------------------------------------------
// ERROR PROCESSING
// ------------------------------------------------------------
private function responseMessage($bool, $msg) {
    $return['error'] = $bool;
    $return['msg'] = $msg;
    if (isset($_POST['plAjax']) && $_POST['plAjax'] == true) {
        $this->jsonErrors[] = $return;
    } else {
        foreach ((array) $msg as $key => $value) {
            $this->phpErrors .= $msg;
        }
    }
    $this->formError = true;
}

I think, the problem is that no matter if it is just a single error message or multiple, the json object is always wrapped with square brackets. I couldn't find anything online that would show an example. The messages look like this:

SINGLE ERROR:

[{"error":true,"msg":"Error message 1 ..."}]

MULTIPLE ERRORS:

[{"error":true,"msg":"Error message 1 ..."},{"error":true,"msg":"Error message 2 ..."}]

Firebug shows 200 OK but my JQuery does not output any messages:

$.ajax({
    type: 'POST',
    url: plSubmitUrl,
    data: plFormData,
    dataType: 'json',
    cache: false,
    timeout: 10000,
    success: function (data) {
        if (data.error === true) {

            // display error message
            plResponse(data.msg, true);

            ...
        } else if (data.error === false) {

            // display success message
            plResponse(data.msg, true);

            ...
        }
    },

When I was showing only one message at a time, the JQuery was working fine.

Any help would be great. Thanks

0

3 Answers 3

1

Dont use strict equality operator === since from PHP you are actually sending string, instad of:

if (data.error === true) 

Use:

if (data.error == true) 
Sign up to request clarification or add additional context in comments.

Comments

0

Since multiple error contains more than one index, so it might not picking data. check length for more than error.

$.ajax({
type: 'POST',
url: plSubmitUrl,
data: plFormData,
dataType: 'json',
cache: false,
timeout: 10000,
success: function (data) {
    var x = data.length;

    if(x == 1){
        if (data.error === true) {

           // display error message
            plResponse(data.msg, true);

        ...
        } else if (data.error === false) {

          // display success message
           plResponse(data.msg, true);
        }
   }else{
       jQuery.each(data,function(key,val){
           // do whatever you need
       }

}

Comments

0
header('Content-Type: application/json');

Add this right before echoeing in your PHP script to let jquery know it has to parse the whole string as JSON.

And use "==" to compare instead of "==="

Comments

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.