我仍然在努力探索JQUERY,AJAX和PHP的问题.
我现在可以调用PHP OK,处理表单元素并发送电子邮件,但我不处理返回AJAX.我总是让error:选择器激活,当我尝试列出所返回的假定JSON时,我得到信息,这显然是错误的.
PHP假定JSON返回
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
Javascript和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", …Run Code Online (Sandbox Code Playgroud) 我试图从php发送"通知"或错误消息到ajax.我正在努力实现这样的目标:
PHP:
if (myString == '') {
// Send "stringIsEmpty" error to ajax
} else if (myString == 'foo') {
// Send "stringEqualsFoo" error to ajax
}
Run Code Online (Sandbox Code Playgroud)
阿贾克斯
$.ajax({
url: $(this).attr("action"),
context: document.body,
data: formData,
type: "POST",
contentType: false,
processData: false,
success: function(){
alert("It works");
},
error: function() {
if(stringIsEmpty) {
alert("String is empty");
} else if(stringEqualsFoo) {
alert("String equals Foo");
}
}
});
Run Code Online (Sandbox Code Playgroud)
如何向ajax发送错误消息?
更新
这是我的php文件.我尝试使用echo解决方案答案说,但当我输出的data是(在ajax中),我得到undefined:
<?php
$img=$_FILES['img'];
if($img['name']==''){
echo('noImage');
}else{
$filename = …Run Code Online (Sandbox Code Playgroud)