Check if email address already exists in the database using jQuery and PHP

Check if email address already exists in the database using jQuery and PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Preventing Form Submission with jQuery and PHP Validation

When building a registration system, one of the most common friction points is allowing a user to submit duplicate accounts. The scenario you described involves intercepting a form submission via jQuery before it reaches its final destination (view.php) to check for existing records in lf_clients. While the intent is sound—validate first, then proceed—the implementation requires careful handling of both client-side interception and server-side security.

The core issue preventing your current setup from working lies in two distinct areas: how the form action interacts with JavaScript events, and a critical vulnerability in your SQL query.

Intercepting the Form Submission

In your HTML, you have defined a standard <form> tag with an action attribute pointing to view.php. When a user clicks the submit button, the browser's default behavior is to immediately send an HTTP POST request to that URL. JavaScript events like .click() fire before the form submission completes, but if you do not explicitly prevent the default action, the browser will still navigate away from the page regardless of what your jQuery code does inside the callback.

To stop the redirection to view.php, you must call event.preventDefault(). Without this, the AJAX request is sent in parallel with the traditional form submission. By the time your PHP script responds with "The email already exists," the browser has already started loading view.php, rendering your feedback invisible to the user.

Securing the Database Query

Your PHP logic attempts to check if an email exists using a direct concatenation of variables into the SQL string:

$sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email'];

This approach is susceptible to SQL Injection attacks. If a user inputs a string like admin' OR '1'='1 into the email field, the resulting query becomes valid SQL that bypasses your logic entirely and exposes all client records. You must use prepared statements with bound parameters to ensure the data is treated strictly as text.

Furthermore, your current code does not handle the AJAX response correctly in the HTML form context. Since you are preventing default submission, the alert(data) call will display the message, but the user cannot interact with it easily if they prefer a UI overlay over an alert box. More importantly, simply echoing "The email already exists" is insufficient for modern web applications; you should return structured data (like JSON) to indicate success or failure codes, allowing your JavaScript to handle the UI updates gracefully.

Implementing the Fix

To resolve this, update your HTML form to include an ID that jQuery can target for event binding, and ensure you prevent the default action:

<script>
$('#Submit').on('click', function(e) {
    var emailVal = $('#email').val();
    
    // Prevent the browser from submitting the form to view.php
    e.preventDefault(); 

    $.post('checkemail.php', {'email': emailVal}, function(data) {
        if (data.success === false) {
            alert("The email already exists.");
            return; // Stop further processing
        }
        
        // If valid, you can now submit the form normally or redirect
        window.location.href = 'view.php'; 
    });
});
</script>

In your checkemail.php, refactor the query to use prepared statements:

<?php
include("../includes/db.php");

$stmt = $con->prepare("SELECT email FROM lf_clients WHERE email = ?");
$stmt->bind_param("s", $_POST['email']);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    // Return a structured response indicating the email exists
    echo json_encode(['success' => false, 'message' => 'Email already registered']);
} else {
    // Email is safe to use
    echo json_encode(['success' => true]);
}

$stmt->close();
?>

Modern Alternatives

While jQuery and AJAX are valid tools for this task, modern Laravel applications often handle this logic server-side using the form validation middleware. By defining rules in your controller or route model binding, you can check for existing emails without writing custom JavaScript interceptors. This approach keeps business logic in one place and ensures that even users who disable JavaScript cannot bypass the security check. However, for standalone scripts or legacy systems where client-side interception is required, the pattern above provides a secure and functional solution.

Our network