I have a custom PHP template - form. I'm trying to send this form in the same page using AJAX without reloading the page. Hide the form after submission and display the thank you message. This form is displayed in a modal. But what happens is that the page is still reloading.


<form method="POST" action="" name="modalForm" id="modalForm" enctype="multipart/form-data" autocomplete="off">
        <input placeholder="Last Name*" type="text" name="lastName" id="lastName" value="" required>
        <label class="error" for="name" id="lastName_error">This field is required.</label>
        <input placeholder="First Name*" type="text" name="firstName" id="firstName" value="" required>
        <label class="error" for="name" id="firstName_error">This field is required.</label>
        <input placeholder="Email Address" type="email" name="Email" id="Email" onblur="this.setAttribute('value', this.value);" value="" required>
        <label class="error" for="email" id="email_error">This field is required.</label>
        <span class="validation-text">Please enter a valid email address.</span>
        <input placeholder="Mobile Number*" type="text" name="contactNumber" id="contactNumber" onkeypress="return isNumberKey(event)" value="" size="11" minlength="11" maxlength="11" pattern ="^09\d{9}$" required>
        <label class="error" for="contactNumber" id="contactNumber_error">This field is required.</label>
        <input type="submit" name="submit" value="Submit" id="form-submit">


    (function($) {
    $(".button").click(function() {
      // validate and process form here

        var name = $("input#lastName").val();
        if (lastName == "") {
        return false;
        var name = $("input#firstName").val();
        if (lastName == "") {
        return false;
        var email = $("input#Email").val();
        if (Email == "") {
        return false;
        var phone = $("input#contactNumber").val();
        if (contactNumber == "") {
        return false;


    var dataString = 'lastName='+ lastName + '&Email=' + Email + '&contactNumber=' + contactNumber;
  //alert (dataString);return false;
    type: "POST",
    url: "/wordpress-page/",
    data: dataString,
    success: function() {
      $('#modalForm').html("<div id='message'></div>");
      $('#message').html("<h2>Contact Form Submitted!</h2>")
      .append("<p>We will be in touch soon.</p>")
      .fadeIn(1500, function() {
        $('#message').append("<img id='checkmark' src='images/check.png' />");
  return false;

Your jQuery event is bound to elements with the class submit, but your submit button in the form doesn't have that class.

Either add that class to the submit button, or just target the form itself. Simply change

$(".button").click(function() {


$("#modalForm").submit(function() {

This will fire when the form is submitted, whether the user clicks the submit button or simply hits enter in a text field.

| improve this answer | |
  • still the page is reloading when form submitted – Yang Jan 26 '18 at 3:25
  • Check for JavaScript errors in your browser's console in the developer tools. – janh Jan 26 '18 at 8:23
  • unfortunately, there's no js error – Yang Jan 26 '18 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.