0

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
  • I would suggest using a middle function that you call 'onclick', and using that to create a callback for when the form is submitted, and then submit the form passing in the callback. Checkout this stack answer: stackoverflow.com/questions/21612197/… Commented Jun 10, 2015 at 19:46

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot. I think that dataReceived in your code have to return a message that shows success or fail. But how and where is that dataReceived function? @hugo-ferreira
And I want to call some functions like window.opener.location.reload(); to reload the mother page. Where should I put this? @hugo-ferreira
for first question, dataReceived function is a anonymous function, you have nothing more to do in client side. just server side, where you have to create a page that will receive the post, validate it, save on database and return a text to be show to user
for seccond question, you can reload page with 'window.location.reload()' after the alert(). BUT, if you want to refresh page after ajax call. why you just dont make a post normally without ajax? it would be more easy to you. keep sophisticated programming style to later after you was expert in easy way ;)
If you like my answer, please vote it as "accepted" ;) thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.