Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Keep data entered on html form after submit with data saved via AJAX

I have an html form:

<form id="formfacility">
    <input type="text" id="MyName">
    <button type="button" id="btnCanxFormFacility">Cancel</button>
    <button type="submit" id="btnSubmitForm">Submit</button>
</form>

then in javascript I have for the submit button:

           $("[id^='btnSubmitForm']").click(function (evt) {
            evt.preventDefault();
            var btnID = "#" + $(this).attr('id');
            var formID = btnID.replace("btnSubmit", "");
            console.log(btnID, formID);
            ProcessFormSubmit(formID, btnID);
        });

where ProcessFormSubmit does the validation and saves the data via AJAX.

for btnCanxFormFacility, I have

        $("#btnCanxFormFacility").click(function () {
            let hideandreset = false;
            let HasChanges = HasLocationDataChanged();
            if (HasChanges) {
                if (confirm("Are you sure you want to cancel and lose your changes?")) {
                    hideandreset = true;
                }
            }
            else { hideandreset = true; }
            if (hideandreset) {
                $('#FormFacility').trigger("reset");
                $("#btnCanxFormFacility").prop("disabled", true);
            }
        });

My issue is that when btnCanxFormFacility is clicked, it deletes all the data that was entered on the form after page load. Normally, this is what is supposed to happen with an HTML reset and would be resolved by the form posting and reloading. However, since I am using JavaScript and AJAX, there is no round trip so the form thinks all of those entries are changes, and clears them.

Is there an easy way to tell the browser not to reset any of the fields that were saved on the last execution of "btnSubmitForm"?

Perhaps I did not explain clearly.

  1. The form is displayed with data from the DB.
  2. The user enters or changes the displayed data.
  3. The user clicks submit and all the data from the form is validated and sent to the DB using AJAX. The form is NOT refreshed.
  4. The user again enters or updates data (I know, right).
  5. User realizes they made a mistake so clicks cancel to clear their entries. The point here should be to only clear entries that have changed since the last submit. Unfortunately, that clears everything since page load. I know that HTML cancel is supposed to do that. But, when we are dealing with ajax, etc. that process outside the normal request/response cycle, there must be a way to deal with that.

Thanks,

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your case, you can add this at the time of the ProcessFormSubmit , either inside the ProcessFormSubmit, for example, if this already has code for the before/after or loop inputs.

The key is:

 $(this_input).prop("defaultValue", $(this_input).val())
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!