0

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();
    }
  });
}
});
});
flowover9
  • 55
  • 1
  • 8

2 Answers2

0

Try adding event.preventDefault(); in your code as follows:

$(document).ready(function(event) {

  $("#submit").on("click",function() {

    event.preventDefault();

    var name = $("#Name").val();
    var email = $("#Email").val();

    ...
Ahsan
  • 1,084
  • 2
  • 15
  • 28
0

I had to pass event in every function, that did it.

$(document).ready(function(event) {

$("#submit").on("click",function(event) {

event.preventDefault();

var name = $("#Name").val();
var email = $("#Email").val();
flowover9
  • 55
  • 1
  • 8