Todavía estoy luchando para entender las entradas y salidas de JQUERY, AJAX y PHP.PHP devolviendo JSON a JQUERY AJAX LLAME
Ahora puedo llamar a PHP OK, procesar los elementos del formulario y enviar un correo electrónico, pero no estoy manejando el retorno al AJAX. Siempre estoy activando el selector error:
y cuando trato de listar el supuesto JSON devuelto, obtengo información, eso es obviamente incorrecto.
PHP con supuesta JSON retorno
<?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 y 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();
};
Listado de supuestos datos JSON
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
Si alguien puede asesorar en cuanto a lo estúpido error que he hecho , entonces estaría muy agradecido.
¿dónde se define '$ json'? Simplemente puede usar 'json_encode();' – Prisoner
¿Qué versión de jQuery está usando? – marc
@marc Versión 1.6.2 en Google CDN – mcl