[Solved] Why is my page reloading on form submission? [closed]


Samuel is correct about the JavaScript error you are getting, but you still are far from getting that form submitted. In your script below you reference variables by the names of “email” and “name”, but these variables do not exist.

  $(document).ready(function(){
    $('submit').click(function(){
      $.ajax({
        type:'POST',
        url: email_form.php,
        data:{
          name: name,
          email: email,
        },
        success: function(msg){
          alert('Email Sent');
        }               
      });
    });
  });

You need to reference the form values. To do so you should use $(‘input[name=”email”]’).val() and $(‘input[name=”name”]’).val() respectively so the code will look like

  $(document).ready(function(){
    $('#submit').click(function(){
      $.ajax({
        type:'POST',
        url: 'email_form.php',
        data:{
          name: $('input[name="name"]').val(),
          email: $('input[name="email"]').val()
        },
        success: function(msg){
          alert('Email Sent');
        }               
      });
    });
  });

Finally I noticed one last thing, your submit button should be of the type “button” not “submit” as the latter will cause a page refresh which will break the running JavaScript.

Note I added the selector error Juan noted to the resulting function.

2

solved Why is my page reloading on form submission? [closed]