0

I have this php that returns an array if an error is triggered, else, it returns a <DIV>

if(!empty($error)) {
   $true = true;
   $res = array('info' => '$error', 'error' => '$true');

   echo json_encode($res);
   die();

} else {
   // no error
   echo "<div> no error </div>";

}

I think my problem lies in the dataType:json parameter because it expects JSON_ENCODED format? I just want to append the <DIV> (non json_encoded) if the else condition is met.

$.ajax
  ({
    type        : 'POST', 
    url         : 'submitForm.php',
    data        : formData,
    dataType    : 'json',
    success     : function(data) {

    if(data.error == true) {
        console.log(data.info); //display error
      } else {
        console.log(data);
        //some jquery to append the <div>
    }
  }
})

Checking the headers appears to be okay, and the preview tab, returns the <DIV> data

Request Method:POST
Status Code:200 OK

But its not appending, Nor the <DIV> being shown in console.log.

Is there a way i could disable the dataType if a certain PHP condition is met? OR, a proper way of handling json_encoded along side with non json_encoded format in the same PHP file?

1
  • 1
    dataType : 'json', is optional, you don't have to explicitly define the data type. Just remove that option. Commented Nov 28, 2017 at 14:25

2 Answers 2

1

just return your html using json too

PHP

if(!empty($error)) {
   $true = true;
   $res = array('info' => $error, 'error' => $true);
} else {
   // no error
   $res = array('html'=>"<div> no error </div>");
}
echo json_encode($res);

HTML

$.ajax
  ({
    type        : 'POST', 
    url         : 'submitForm.php',
    data        : formData,
    dataType    : 'json',
    success     : function(data) {

    if(data.error == true) {
        console.log(data.info); //display error
      } else {
        console.log(data);
        $('div').append(data.html);
    }
  }
})
Sign up to request clarification or add additional context in comments.

3 Comments

This is a much more proper way to handle it. Dealing with mixed outputs can cause headaches down the line, and makes the code hard to read. So do what this answer says, stick to a format, and use it for both output conditions.
Also worth noting '$error' isn't going to contain what the OP wants. Should not be quoted, or use double quotes.
it took me a while to edit my script, my <div> example was actually simplified, it had more underlying PHP code beneath that. Your answer did point me to the right direction. I Just realized that it really is hard to combine json_encode with non-json_encode scripts. I just didnt have too much time to re-code everything. But this is a lesson for me, to carefully approach such situations.
0

Using json for errors and html (or pseudo html) for regular responses struggles a bit to me.

That being said, if you really want to do that, you obviously can't use dataType: 'json' option because this instructs $.ajax() to expect (and parse) json data so, if what is received is not a valid json string, then an exception will be thrown.

But anyway you can emulate it by parsing json data yourself. For example:

dataType: "text",
success: function(data) {
    try {
        data = JSON.parse(data);
        if (! data.error) { // What if you send json witout error flag?
            data.error = true;
            data.info = "Received json without error info."
        };
    } catch (e) {
        data = {
            contents: data,
        };
    };

    if (data.error) {
        console.log(data.info);
    } else {
        console.log(data.contents);
        // Or whatever you want to do wit received STRING
},

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.