问题描述
我不认为我传递变量我单独的PHP和AJAX文件之间的正确途径。
I don't think I am passing the variable the right way between my separate PHP and AJAX files.
我通过触发第二个条件 $状态='信息'调试此。
I am debugging this by triggering the second condition
$status = 'info';
in my PHP file.
目前,
状态
快到了为不确定的警报(data.status);
Currently,
status
is coming up as "undefined" for alert(data.status);
signup_process.php
signup_process.php
if (condition){
$status = 'success';
else {
$status = 'info';
}
AJAX
AJAX
function send() {
var data = $('#signup_form').serialize();
$.ajax({
type: "POST",
url: "signup_process.php",
data: data,
success: function (data) {
alert(data.status);
if (data.status == 'success') {
// everything went alright, submit
$('#signup_form').submit();
} else if (data.status == 'info')
{
console.log(data.status);
$("label#email_error").show();
return false;
}
}
});
return false;
};
我知道,第二个条件被触发,因为我把一个头重定向那里只是用于测试和它工作得很好。
I know that the 2nd condition is being triggered because I put a header redirect there just for testing and it worked fine.
推荐答案
很好用的 JSON ,而从PHP数据返回到AJAX。
Good to use json while return back data from php to ajax.
$return_data = array();
if (condition){
$return_data['status'] = 'success';
} else {
$return_data['status'] = 'info';
}
echo json_encode($return_data);
exit();
现在,如果你是JSON数据返回到阿贾克斯,那么你就需要为指定返回的数据类型为阿贾克斯拨打如下
Now, if you are return back json data to ajax, then you need to specify return data type into ajax call as below
function send() {
var data = $('#signup_form').serialize();
$.ajax({
type: "POST",
url: "signup_process.php",
data: data,
dataType: 'json',
success: function (data) {
alert(data.status);
if (data.status == 'success') {
// everything went alright, submit
$('#signup_form').submit();
} else if (data.status == 'info')
{
console.log(data.status);
$("label#email_error").show();
return false;
}
}
});
return false;
};
这篇关于我怎样才能获得一个PHP变量AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!