41

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.

6
  • where is $json defined? You can just use json_encode(); Commented Aug 15, 2011 at 11:31
  • What version of jQuery are you using? Commented Aug 15, 2011 at 11:33
  • @marc Version 1.6.2 on Google CDN Commented Aug 15, 2011 at 12:17
  • @Prisoner. I changed to your suggestion. I was obviously wrong. Copied code again, but still not working as I had hoped - same listing of spurious values Commented Aug 15, 2011 at 12:27
  • There should be an output status=... what is the value there? Commented Aug 15, 2011 at 12:29

1 Answer 1

116

You can return json in PHP this way:

header('Content-Type: application/json');
echo json_encode(array('foo' => 'bar'));
exit;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the tip. I tried and I am getting the same result. Why is error: always being executed ?
Many thanks. I now understand why and you were right. Now fixed and working
I added exit as there should be nothing after the JSON response, hope you don't mind.

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.