tags:

views:

562

answers:

4

I have a moronic customer that does not seem to understand the English text in an alert that instructs him that his data was saved successfully. He continually presses the submit button and inserts a new row of data into the database. Can anyone suggest a solution to prevent this please? I am using jQuery to submit the data so there is no refresh. Thanks.

+1  A: 

Generate a unique token on the page from which the insert is occurring in a hidden field that will be posted when the form is submitted. Store this token in the database somewhere. When a post containing that token is processed, mark that token has having been consumed if it hasn't already been marked. If a post with a token that has already been consumed is seen, refuse to process it and present the user with an error (or redirect to an edit/update page so they can make changes).

EDIT: based on comments

If you need multiple inserts from the same page, then presumably you have some input fields that contain the new data. When you go to submit the data, put up a modal dialog that says "please wait while I update your data". This will prevent the user from doing any actions while the update is happening. When the update returns successfully clear the input fields and dismiss the dialog. If the request takes too long, dismiss the dialog, show an error, and leave the data in the fields for re-submission.

tvanfosson
Hi tvanfosson. Thanks for the direction on this. I was also thinking the same thing. Kind of like a session id, no? I'm thinking that if I use a session to do this with, that session will remain active until the user closes their browser. How can I get around this?
Get around what? If the token is uniquely generated for each page (like a GUID), then it will only affect that page. Also, if there is something unique about the data being entered you could put a unique index on that column to prevent duplicate entry. That's really data-dependent though and make not work in all cases.
tvanfosson
Ok, what I am talking about is a JQ submit where there is no refresh on the pasge at all. Say that the user opens the form to enter the data. If I am understanding you correctly, when the page opens, I am going to generate a guid and insert that into the database. The user will then enter the data on the page that has that guid in a hidden field. When the user wantes to enter a new row of different data, won't the guid still be the same? The page does not refresh, it's jquery. Do you see my dilemma? I'm sure its me and I'm not seeing something. Please fill in the gaps for me please.
Okay. Presumably you have some input fields that contain the new data. Throw up a modal dialog that says "please wait while I update your data". When the update returns successfully clear the input fields and dismiss the dialog. To submit the same data, the user will have to enter it all again. If they do that, then nothing you can do -- short of having a unique index on all columns -- will prevent them from re-entering the same data. Will update my answer with this.
tvanfosson
The user most of the time never leaves that page. They will stay on that same page, sometimes entering 50 or more rows of different data. This customer has a lot of data to enter and has hired a data entry person so that is what they do all day long. How would I kill the guid if the user never leaves the page?
Thank you tvanfosson. Great answer!
+4  A: 

You can disable the submit button or image until the request returns a result.

Matt Hinze
Hi Matt. What happens after the result is returned and the button reopens to the user? They can press it again. No?
So... think about that user experience. What needs to happen for him to be comfortable NOT clicking it again? Does he need to see the values confirmed on the page somewhere? Does he need to see a list of the 4 most recently added items? What exactly is the problem that's inciting that behavior. Once you figure that out, you can fix it.
Matt Hinze
Thanks for the reply Matt. The issue is that JQuery does not "clear" the data from the textboxes in the traditional sense before there was JQuery we would have the data automatically cleared with the page refresh. Because it does not refresh, the data stays. To her (customer) credit, it can be a bit confusing when you submit the data and its still there.
You can have jquery clear the inputs in the callback function: something like $('input').val('');
Matt Hinze
A: 

You could put that data on screen, use a yellow fade effect to highlight it on screen in a list view. As the new data is inserted to the top of a table or list element, highlight it temporarily, use jquery's new live methods to add in the ability to remove the row through AJAX.

Also since you just got the data you don't have to wait to add it to the screen just make sure you check the database gives a successful result back so you can remove it or use a placeholder till the database returns success full then add the data into it on success.

joelpittet
Hi Joel. Thanks for the help. Unfortunately, I'm not using a grid type interface on this. These are the traditional textboxes. I like the idea a lot though.Regarding the "callback" (I'm not even sure this is what it may mean to you) How do I get a callback, whether successful or unsuccessful back into the html? I am using a php class to process the data and having that method return true on success and false on failure. Once that has been tested, how do I forward that back to the user?
+1  A: 

Wordpress has a great animation when you delete a post via AJAX. Maybe you could do something similiar, except add the new row on screen (use JSON to get the data back) and fade it in, green first to help show the end user it is green.

As for not resetting the form values, you could do something like this in jQuery

$('#myForm input[type=text]').val('');

Good luck Frank!

EDIT I think if you return the results via AJAX, and then update something on the screen, the end user will be less likely to think his input hasn't been entered into the db.

You can get a cool ajax loader from www.ajaxload.info, and display this whilst the AJAX (I'm assuming when you say the user doesn't leave the page, that is what you're referring to) is processing.

alex
Hey Bro.. Thanks for stopping by. :) Looks like I made the 15 rep already. hehe. Now to stop the customer from douple and triple posting data.
Will this clear the values on submit?
If you attach that line of jQuery to the submit event (or whatever event you use), it will clear all textboxes from a form with an ID of myForm
alex
Alex. here is my code. $.ajax({ type: "POST", url: "index.php", data: dataString, success: function(){ $('#contact input[type=text]').val(''); alert( "Success! Data Saved"); } });This doesn't clear the form though. Am I doing something wrong?
I wouldn't quite know without seeing a bit more of your code. It should work. Do you get that alert() ?
alex
Just clearing the form is cool with me. I don't need all the modal windows and back scrubbers going on. :)
I do get the alert, yes but the form doesn't clear.
Well I'd change that val('') to something like css('border', '1px solid red') and if your text inputs don't grow a red border, then your selector is probably incorrect.
alex
Um.. I really new at JQ.. :) Do you mean like this?$('#myForm input[type=text]').css('border', '1px solid red');
I just tried that anyhow but the boxes didn't have a red border. :( My form tag:<form name="contact" method="post" action="">
okay, on your form tag, you need to add id="contact" and then use the jQuery selector '#contact input[type=text]' :)
alex
hehe... You good my friend. :) Thanks! I'm really starting to fall in love with JQ.Hey, let me ask you a question and we can even start a new thread if you want to.. but what I was wanting to know was how to show the user a pretty little success window if the data was *truly* saved.
Okay, ask the question, and I'll give an answer
alex