ayush-that / FinVeda

A web application designed to enhance financial literacy.
https://fin-veda.vercel.app/
GNU General Public License v3.0
67 stars 119 forks source link

Enhancements and Bug Fixes for AJAX Contact Form Submission #292

Open Swapnilden opened 2 weeks ago

Swapnilden commented 2 weeks ago

1. Unspecified Action Attribute

2. Handling Different Response Formats

3. jQuery Dependency

4. Lack of Client-Side Validation

5. No Loading Indicator

6. Inadequate Error Handling

Proposed Code Improvements

Below is the improved version of the script addressing the above issues:

$(function() {
  var form = $("#contact-form");
  var formMessages = $(".form-message");
  var submitButton = form.find("button[type='submit']");

  form.submit(function(e) {
    e.preventDefault();

    // Client-side validation
    if (!form[0].checkValidity()) {
      formMessages.removeClass("success").addClass("error").text("Please fill out all required fields correctly.");
      return;
    }

    // Show loading indicator and disable submit button
    submitButton.prop("disabled", true).text("Sending...");

    var formData = form.serialize();

    $.ajax({
      type: "POST",
      url: form.attr("action"),
      data: formData,
      dataType: "json", // Expect JSON response
    })
    .done(function(response) {
      formMessages.removeClass("error").addClass("success").text(response.message);
      form[0].reset();
    })
    .fail(function(data) {
      formMessages.removeClass("success").addClass("error");

      // Check for specific error messages
      if (data.responseJSON && data.responseJSON.errors) {
        var errors = data.responseJSON.errors;
        var errorMessage = "Please fix the following errors:\n";
        $.each(errors, function(key, message) {
          errorMessage += key + ": " + message + "\n";
        });
        formMessages.text(errorMessage);
      } else {
        formMessages.text(data.responseText || "Oops! An error occurred and your message could not be sent.");
      }
    })
    .always(function() {
      // Hide loading indicator and re-enable submit button
      submitButton.prop("disabled", false).text("Send Message");
    });
  });
});
github-actions[bot] commented 2 weeks ago

Hi there! Thanks for opening this issue. We appreciate your contribution to this open-source project. We aim to respond or assign your issue as soon as possible.

Swapnilden commented 2 weeks ago

Please assign me this issue under GSSOC'24