View Full Version : Auto Save Form Values issue / enchancement

07-31-2012, 10:36 PM
1) Script Title: Auto Save Form Value

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex16/autosaveform.htm

3) Describe problem: I have a complex Service Management program that I wrote, that is used internally in my business. It is mostly PHP, with a little bit of HTML on the forms. One of my users has a very bad browser habit, in that he likes to open all kinds of links using tabs. His bad habit is that he will close the browser, and lose the data he had entered in the service management program. He will open my program, start adding data to the Work Order he created for the piece of electronics he is working on, and go off in other directions, leaving the Work Order open and -not- saved. He closes the browser and BAM!, all his Work Order data is lost. I've told him that this is really a user issue but he argues that software should protect the (stupid) user. Hence, I went off on a search to do like MS Word does, which asks if you want to save, on exit. Unfortunately, this is *not* a feature built-in to browsers !!

So, when I found your script, it was just GREAT. Easy to implement, and fairly straight forward to understand. Works exactly as 'advertised'. NICE !!

So the problem: I have up to 5 buttons on a form, one that saves via Submit button, and others that are -not- type="submit" but type="button". I can change type="button" to "submit", and when the user clicks that, it cancels your Auto Save Form script like it is supposed to. The issue I have is that the form runs the usual Submit code, and *NOT* the code associated with that buttons OnClick event. Basically, the Submit event fires before the OnClick event, running the wrong code. Apparently, this is normal browser behavior.

So the issue/enhancement would be to have a programmatic method to cancel the autosave functionality -or- possibly a custom ID or CLASS tag associated with a button, that would also cancel the autosave.

Any ideas ??

Thanks !!


08-01-2012, 03:37 AM
If I'm reading this write, the problem is that it submits before the onclick event. You could replace it with onsubmit, which I'm pretty sure is designed to fire before the form submits.

08-01-2012, 03:52 AM
I'm not sure I understand. But it seems as though you're saying that if you change your <input type="button" to an <input type="submit" that it fires/submits the form without running the onclick event that's associated with the input.

There's a relatively easy solution for that - say you had:

some form elements
<input type="button" onclick="dosomething();" value="Go">

You can change that to:

<form onsubmit="dosomething(); return false;">
some form elements
<input type="submit" value="Go">

There are various variations depending upon what you need it to do. For example, if you want it to submit after runnng dosomething(), you can change the return false; to return true;.

If you want more help, make up a live demo that shows the problem and give us a link to it.

08-01-2012, 05:08 PM
Bernie - Thanks, I will try replacing the OnClick w/OnSubmit and let you know

JScheuer - I have a Save button, that is a <input type="submit"> button which does the form submit and cancels the autosave. I *also* have <button type="button" OnClick="SomeJavescriptCode()"> buttons. On these, I changed type="button" to type="submit" but as described, the 'Submit' event fires before the 'OnClick' fires, "diverting" that click to the wrong code. I will try both you guys suggestions and a MILLION THANKS !!


08-02-2012, 04:16 PM
Ok, so using OnSubmit didn't work. It still does a standard form Submit.

So, I've taken a different approach to solve the problem, which is to leave Auto Form Values Save as it is, and modify the HTML using JS.

Basic approach is to make ALL buttons on the form into Submit buttons, which will satisfy the DD script, and to modify the form Action parameter via JS. A JS function will select which form name, and which variables to pass to that form, by distinguishing which button was pressed.

Thanks for all of your help !!