I submit a php form using jquery ajax, but when receive a parameter value of callback function success, the parameter value contains enter key in front of the value returned from the server.
The work around, I use includes method.
Does anyone know why the enter key inserted in front of the value returned from the server?
$.ajax({
type: "POST",
url: "_action.php",
data: dataString,
success: function(result) {
n = result.includes("success");
if (n) {
grecaptcha.reset();
$("#spinner-contact").remove();
$('#success_msg').html("<img class='img-fluid aligncenter mb-3' id='checkmark' src='media/img/form_submitted.jpg' />");
}
else {
grecaptcha.reset();
$("#spinner-contact").remove();
$('#success_msg').html("<div class='mt-3'><strong><h1 class='alert alert-warning'>Contact failed!</strong> Please submit again.</h1></div>");
}
}
});
$statement = runQPs("INSERT INTO my_guestbook(title, url, path, pos, content, isactive, date) VALUES(
?, ?, ?, ?, ?, ?, ?
)",[$name, $email_from, $address, $telephone, $comments, 0, $now]);
// Insert contact success or fail
if($statement->affected_rows === 0) {
$result = "fail";
}
else {
$result = "success";
}
echo $result;
3
Answers
After echo the result variable you can die the statement something like that.
or
i think, u can change your "echo" in php file to return "json"
and in your ajax code:
to
In PHP code you can return value like that:
In JS file: