My JS function expects a certain return from the PHP code in case of a failure.
function showResult(data)
{
if (data == 'save_failed') {
document.getElementById('result').innerHTML = 'Unfortunately, we were not able to save your information. Please contact an admin.';
return false;
} else {
$("#notify").clearForm().clearFields().resetForm();
document.getElementById('result').innerHTML = 'Thank you for signing up with us.';
return false;
}
}
This works fin when I echo 'save_failed'; in case of an explicit error, but it does not work in the case of die statements, such as this one:
mysql_connect("host", "user", "pass") or die('save_failed');
I entered a wrong hostname but the JS function did not receive the 'save_failed' return.
PHP script:
if (!(empty($_POST['name']) && empty($_POST['email'])))
{
//sanitizing inputs for MySQL insertion
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
//connecting to db
mysql_connect("host", "user", "pass") or die('save_failed');
mysql_select_db("table") or die('save_failed');
//inserting into table
mysql_query("INSERT INTO notify (name, email) VALUES('" . $name . "', '" . $email . "' ) ")
or die('save_failed');
}
else
{
echo 'save_failed';
}
Even though
die("save_failed")will output “save_failed”, it will not stop any previous output in your script from being sent.Ensure that if you go to the script yourself then you are receiving just “save_failed” in the page source and no erroneous characters