+3  A: 

It has to be clicked twice because you are defining #microsubmit's click event inside the function. So the first time you click you bind the event handler, and the 2nd time the event handler is in place and gets fired. I haven't gone over the logic behind what you're trying to accomplish but my guess is that if you move the event binder outside the function and make sure all your variables are in the right scopes then it'll work.

Paolo Bergantino
+1  A: 

The first time you load the page, the click handler is not hooked to the button, is only until you click the button the first time that you are calling the niju() and hooking the click event. You need to do something like

$(document).ready() { niju(); }

and remove the onclick from the button declaration

Jaime
A: 

Move your flag out of the function niju.

 var flag=1;
function niju()
{
}
rupert0