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. 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. I know that from my PHP file I can echo something correctly. But if I return FALSE then the AJAX success function doesn't take that.
Here is my AJAX POST call code:
$.post("addCustomer.php",
{...DATA...} ,
function(data){
if (data){
$("#myForm").trigger("reset");
}
});
Here is my PHP file:
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...
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.
header('Content-Type', 'application/json');
echo json_encode(['success' => createMember(...)]);
exit();
and in js you can simply use something like this
$.post("addCustomer.php",
{...DATA...} ,
function(data){
if (data.success){
$("#myForm").trigger("reset");
}
});