I have a simple form which sends name and email to my database. Jquery is listening to the form and when validated sends the data to form.php .
When I set a break point in firefox dev tools, form.php will be called with the correct data. Otherwise, what I THINK is happening, is that form.php gets called BEFORE jquery manages to send through any data (and this can be understood by the fact that only when I set a breakpoint does the first part of the code be read). I think something like this is happening How do I return the response from an asynchronous call? , I have read it but wouldn't know how to implement these solutions in my code.
$(document).ready(function() {
$("#submit").on("click",function() {
var name = $("#Name").val();
var email = $("#Email").val();
if (name == '' || email == '') {
alert("Please Fill Required Fields");
} else {
$.post("form.php", {
name1: name,
email1: email
}, function(data) {
$("#returnmessage").append(data);
if (data == "Thanks a lot!") {
$("#form")[0].reset();
}
});
}
});
});