<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Juozas devBlog &#187; callback</title>
	<atom:link href="http://dev.juokaz.com/tag/callback/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using AJAX with onSubmit event in jQuery</title>
		<link>http://dev.juokaz.com/javascript/using-ajax-with-onsubmit-event-in-jquery</link>
		<comments>http://dev.juokaz.com/javascript/using-ajax-with-onsubmit-event-in-jquery#comments</comments>
		<pubDate>Tue, 10 Mar 2009 17:09:12 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[submit]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=333</guid>
		<description><![CDATA[Last week I was working on one task, which required me to check data with AJAX when form is being submitted. Users clicks the button, request is made, data arrives, some hidden fields are modified, actual submit happens. The problem I ran into is, how to control form submission and ensure that it&#8217;s submitted only [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I was working on one task, which required me to check data with AJAX when form is being submitted. Users clicks the button, request is made, data arrives, some hidden fields are modified, actual submit happens. The problem I ran into is, how to control form submission and ensure that it&#8217;s submitted only when AJAX has finished it&#8217;s job.</p>
<p>What I had, was a form, where user enters username using auto-completion. Problem was, setting <em>user_id</em> hidden field after user selection (from search results) was sometimes faulty. If users enters &#8220;Joh&#8221;, selects &#8220;John&#8221;, <em>user_id</em> is set to 2. But if user changes value to &#8220;John J&#8221;, selection even is not executed and <em>user_id</em> remains 2. This is a problem, because this is not the users meant and on submit I need to check if given username is correct, and in this case set <em>user_id</em> to <em>null</em>.</p>
<p>It&#8217;s quite easy to forget what A in AJAX stands for. A means asynchronous, which means that when HTTP calls are made JavaScript doesn&#8217;t wait for it to return response. This property made AJAX so useful, but in some cases it&#8217;s not what developers want. For example in jQuery, all AJAX functions has possibility to supply it with anonymous function &#8211; callback.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>
		url<span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// callback with data</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Again, this is what most of the times developers need, but I was working on different problem. Problem is, callback function here will <strong>never be executed</strong>. JavaScript will make a call, go to &#8220;return true;&#8221; statement, and browser is sent to other page. In my case, I needed form to be submitted only when response data has arrived and some manipulations have been done.</p>
<p>It may seem quite an easy problem, but it turned not to be. The problem is, you need to modify this even function, such that it can be called again when callback function have done it&#8217;s work (or modify it, to unbind submit event function and when submit again). First time, function creates HTTP request and returns false (to stop browser from actually submitting it).</p>
<p>After data has completed loading, callback function calls submit event again. However, now function knows that it&#8217;s second call (hence, callback has been called) and returns true &#8211; browser submits the form. What I came up with, is this code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">formLoading</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>
			url<span style="color: #339933;">,</span>
			<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// callback with data</span>
				$.<span style="color: #660066;">formLoading</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It works perfectly. You can test it <a href="http://dev.juokaz.com/examples/jquery/submit/call.html">here</a>: first form works correctly, second one never finishes loading (have a look at code).</p>
<p>I believe, that AJAX on submit is not the best way to do such things, but required functionality was for administration panel, so there was no risk of users cheating. It was possible to do the same work in server-side when handling submitted data, but for better user experience, I wanted to make data check without refreshes.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/javascript/using-ajax-with-onsubmit-event-in-jquery/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
