call a javascript function after a submitted form data is written to DB

344 views Asked by At

My question maybe looks awkward. But I strongly need the answer. Assume a form submit like:

<input name="button2" type="submit" class="test" id="button2" value="submit"
       onclick="checkSubmitStatus();"/>

When the checkSubmitStatus function is called, the form is not submitted yet, so the form data is not arrived in database in server side yet. How can I make them execute in vice-versa? I mean firstly, the form submits and its data is inserted to the server database and then call that javascript function.

1

There are 1 answers

5
Hugo Ferreira On

I think you want to give to user some feedback about if data was inserted successfully or not, right? if so, I recomend you to follow different way. Eg.:

Your markup:

<form id="frm-post" method="post">
<input name="foo" value="bar" type="text" />
<button type="button" id="btn-submit">Submit</button>

Your Code:

$("#btn-submit").click(function(){

var objData = {
    type        : 'post' ,
    url         : 'your url' ,
    data        : $('#frm-post').serialize(),
    dataType    : 'text' ,
    success     : function ( dataReceived ) {
        alert(dataReceived)
    }
}

$.ajax(objData);

});

Considering the page you'll call will return just the message you want to show to the user.

I've not tested the code, but I think it will put you in the right way.