Form Validation and Submission

Rumman Ansari   Software Engineer   0000-00-00 00:00:00   32 Share
Subject Syllabus DetailsSubject Details
☰ Table of Contents

Table of Content:


Form Validation and Submission

Objective: Validate form input in real-time using AJAX before submission.

HTML (form.html):


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Validation Example</title>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <script src="form-script.js"></script>
</head>
<body>
    <form id="myForm">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" onblur="validateUsername()">
        <span id="username-error"></span>
        <br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

PHP (process-form.php):


<?php
// process-form.php - Simulating form processing
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST["username"];
    // Perform actual form processing/validation here
    echo "Form submitted successfully by: " . $username;
}
?>

JavaScript (form-script.js):


function validateUsername() {
    // Using jQuery for AJAX request
    var username = $('#username').val();
    $.ajax({
        url: 'validate-username.php',
        type: 'POST',
        data: { username: username },
        success: function(response) {
            // Display validation result
            $('#username-error').text(response);
        },
        error: function(error) {
            console.log('Error:', error);
        }
    });
}

// Using jQuery for form submission with AJAX
$(document).ready(function() {
    $('#myForm').submit(function(e) {
        e.preventDefault(); // Prevent the default form submission
        // Additional form submission logic or AJAX request if needed
        // For simplicity, we'll simulate form processing with a regular submission
        this.submit();
    });
});

PHP (validate-username.php):


<?php
// validate-username.php - Simulating username validation
$username = $_POST["username"];
if (strlen($username) < 5) {
    echo "Username must be at least 5 characters long";
} else {
    echo ""; // Empty string indicates no validation error
}
?>

Explanation:

  • The validateUsername JavaScript function is triggered on blur of the username input.
  • It sends an AJAX request to validate-username.php for validation.
  • The PHP file simulates username validation and returns a validation message.
  • The JavaScript function updates the error message on the page in real-time.
  • The form, when submitted, is processed by process-form.php on the server.

This example showcases how AJAX can be used to validate form input in real-time, enhancing the user experience.

These examples provide a glimpse into the powerful synergy between PHP and AJAX in building dynamic and interactive web applications. The possibilities are vast, and developers can leverage this combination for various use cases to create modern and responsive web solutions.