tags:

views:

50

answers:

3

hi guys, I have a button.In its click i hve written code for insertion.If i click on refresh in browser again insertion takes place and so the duplicate data enters to database.Is there any javascript to avoid insertion on refresh.Can anybody help?

A: 

To prevent a button from submitting the form, you should make the event handler return false. Like this:

function handler()
{
    // do whatever
    return false;
}

<input type="button" onclick="handler" />

This is just one approach. Depending on how you're using the listeners, you could also use e.preventDefault().

Reinis I.
A: 

Well if you are inserting you should be checking to make sure the data you are entering doesn't already exist within the db, so that should prevent it anyway.

Otherwise is the button submitting the form? Or is it an Ajax call?

Psytronic
+1  A: 

See the Post/Get/Redirect pattern for a general solution to this problem.

karim79