[php] PHP returning JSON to JQUERY AJAX CALL

I am still struggling to get my head around the ins and out of JQUERY, AJAX and PHP.

I can now call the PHP OK, process the form elements and send an email, but I am not handling the return to the AJAX. I am always getting the error: selector activated and when I try to list the supposed JSON returned, I get info, that is obviously wrong.

PHP with supposed JSON return

<?php

touch('phpTouch.txt');
// process email
$email=1;
if ($email) {
    $value = array('return' => 1, 'msg1' => 'Message sent OK, we will be in touch ASAP');
} else {
    $value = array('return' => 0, 'msg1' => 'Message Failed, please try later');
}
$output = $json->encode($value);
echo $output;

?>

Javascript and AJAX

function submitForm(evt) {
    $('#msgid').html('<h1>Submitting Form (External Routine)</h1>');
    if ($('#formEnquiry').valid() ) {
        $("#msgid").append("<h1>(Outside Ready) VALIDATED send to PHP</h1>");
            $.ajax({
            url: "ContactFormProcess3.php",
            type: "POST",
            data: $('#formEnquiry').serialize(),
            dataType: "json",
            success: function (data) {
                alert("SUCCESS:");
                for(var key in data) {
                    $('#msgid').append(key);
                    $('#msgid').append('=' + data[key] + '<br />');
                }
            },
            error: function (data) {
                alert("ERROR: ");
                for(var key in data) {
                    $('#msgid').append(key);
                    $('#msgid').append('=' + data[key] + '<br />');
                }
            }
        });
    } else {
        $('#msgid').append('<h1>(Outside Ready) NOT VALIDATED</h1>');
    }
    evt.preventDefault();
};

Listing of supposed JSON data

readyState=4
setRequestHeader=function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}
getAllResponseHeaders=function (){return s===2?n:null}
getResponseHeader=function (a){var c;if(s===2){if(!o){o={};while(c=bF.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c}
overrideMimeType=function (a){s||(d.mimeType=a);return this}
etc etc 

If anyone can advise as to what stupid mistake I have made, then I would be most grateful.

This question is related to php json jquery

The answer is


You can return json in PHP this way:

header('Content-Type: application/json');
echo json_encode(array('foo' => 'bar'));
exit;

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?