Form submit with AJAX passing form data to PHP without page refresh

Form submit with AJAX passing form data to PHP without page refresh

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Form submit with AJAX passing form data to PHP without page refresh

The core issue preventing your code from working lies in how HTML forms and browser event handlers interact when you mix standard submission logic with asynchronous JavaScript. In your specific example, the form contains an <input type="button"> for the action labeled "Submit". While this might seem like a minor styling choice, it fundamentally breaks the AJAX workflow because standard form submissions are triggered by buttons of type="submit", not type="button".

When you click an element with type="button", no default form submission event fires. Your jQuery handler listens for the submit event on the <form> tag itself. Since clicking the button never triggers that event, your AJAX function is never called, and the browser has no reason to send a request to post.php. To fix this, you must change the input type to type="submit" so the browser recognizes the intent to submit the form data.

<input name="submit" type="submit" value="Submit">

However, simply changing the button type introduces a new problem: page refresh. When a <form> submits via a standard POST request, the browser navigates to the response URL (or reloads the current page if the response is HTML). This breaks the single-page application feel you are aiming for. You need to intercept the default submission behavior and handle it entirely within JavaScript.

Your current code attempts this with return false; inside the event handler. In older jQuery versions or when bound directly to the submit event, this prevents the default action. However, a more robust pattern involves preventing the default action explicitly using event.preventDefault(). This ensures that even if other logic modifies the return value, the form never leaves the current page.

$('form').on('submit', function (e) {
  e.preventDefault(); // Stop the browser from refreshing or navigating
  
  $.ajax({
    type: 'POST',
    url: 'post.php',
    data: $(this).serialize(),
    success: function (response) {
      alert('form was submitted');
      console.log(response); // Check for actual server response
    },
    error: function (xhr, status, error) {
      console.error('Submission failed:', error);
    }
  });
});

There is also a critical detail regarding the form data serialization. Your original code used $('form').serialize(). While convenient, this method serializes all input fields within the matched form element. If you have multiple forms on a page or if the selector matches elements inside a container that holds other inputs, unintended data might be sent to post.php. Using $(this).serialize() ensures that only the fields belonging to the specific form instance triggering the event are included.

On the server side, your PHP script will receive these values in the $_POST superglobal array. If the request fails silently (no JavaScript error thrown), the most common culprit is often the response type or an unhandled exception in your PHP logic. Ensure that post.php returns a valid status code and considers echoing a JSON response if you plan to parse it later, rather than relying solely on an alert for debugging.

For larger applications where maintaining raw jQuery AJAX calls becomes cumbersome, frameworks like Laravel provide built-in mechanisms to handle form submissions seamlessly without writing custom AJAX logic in every view. Tools like route::post combined with Blade directives allow you to define endpoints that automatically handle CSRF protection and data validation. If you are building a modern application stack, leveraging such frameworks ensures security and scalability while abstracting away the manual event binding and serialization details found in the basic example above.

Our network