/    Sign up×
Community /Pin to ProfileBookmark

Event handler that does things only after form information is sent off to the server?

Is there an event handler that I can use to change something in a form *only* after the information typed into the form has been sent out to the server? I don’t want to check for something at the server side, then send something back, because of lag. I tried onsubmit(), but that seems to cause things to happen when the button is clicked, before anything is sent out to the server.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@cgishackJun 28.2008 — do you mean something like this ?

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function changeValue()
{
var name = document.getElementById('username').value;
alert("Hello "+ name + ", we are now submitting the form");
}


&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="form1" name="form1" method="post" action="" onsubmit="changeValue();"&gt;
&lt;label&gt; Name:
&lt;input type="text" id="username" /&gt;
&lt;input type="submit" name="button" id="button" value="Submit" /&gt;
&lt;/label&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;



Drew
Copy linkTweet thisAlerts:
@Declan1991Jun 28.2008 — What you really want is AJAX I'd say, but I'm not sure.
Copy linkTweet thisAlerts:
@rgrneauthorJun 28.2008 — Thanks for your replies.

cgishack: I tried that already. Unfortunately onsubmit() insists on making the changes to the form before the form is submitted, which screws things up.

Declan 1991: Yes, I can do this with Ajax, but Ajax involves waiting for word from the server to say that it has received information before changing the form. This introduces lag, which I don't want. Something that just waited for the data to be on its merry way before doing anything would be best.
Copy linkTweet thisAlerts:
@cgishackJun 28.2008 — So you mean you want a function to fire while the server is processing a request?

This can be done i[B]f you are using Ajax[/B].

If you are not using ajax you can not fire Javascript because the page is reloading at that time and generally JS knows nothing about the previous page.
×

Success!

Help @rgrne spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...