[英]How do I pull in the return data from PHP to give to my AJAX POST call function?
Okay, this one is a bit confusing. 好的,这有点令人困惑。
I have a form that submits to a database. 我有一个提交到数据库的表格。 I use the AJAX POST function to do so. 我使用AJAX POST函数来做到这一点。 Within AJAX the success function needs to have an if statement that says "If the data returned by the PHP file is true clear out the form, if its false leave intact" .. I've been trying to fix this for a while now and I am unable to. 在AJAX中,成功函数需要有一个if语句,该语句表示“如果PHP文件返回的数据是正确的,则清除表格,如果错误保留完好无损” ..我已经尝试了一段时间,现在,我做不到 I know that from my PHP file I can echo something correctly. 我知道从我的PHP文件中,我可以正确地回声。 But if I return FALSE then the AJAX success function doesn't take that. 但是,如果我返回FALSE,则AJAX成功函数不会接受该功能。
Here is my AJAX POST call code: 这是我的AJAX POST呼叫代码:
$.post("addCustomer.php",
{...DATA...} ,
function(data){
if (data){
$("#myForm").trigger("reset");
}
});
Here is my PHP file: 这是我的PHP文件:
public function createMember(...DATA...){
$mysql = new Mysql();
$cuResult = $mysql->addNewCustomer(...DATA...);
if ($cuResult){
return true;
} else {
return false;
}
}
As you can tell, I removed some unnecessary info. 如您所知,我删除了一些不必要的信息。 Everything works, except my IF statement in my POST call... 一切正常,除了POST调用中的IF语句...
I might be doing it wrong. 我可能做错了。 What's the right way to do this? 什么是正确的方法?
I would suggest to return an json string with the necessary information. 我建议返回带有必要信息的json字符串。
header('Content-Type', 'application/json');
echo json_encode(['success' => createMember(...)]);
exit();
and in js you can simply use something like this 在js中,您可以简单地使用类似这样的内容
$.post("addCustomer.php",
{...DATA...} ,
function(data){
if (data.success){
$("#myForm").trigger("reset");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.