<?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>Query7 &#187; form</title>
	<atom:link href="http://query7.com/tag/form/feed" rel="self" type="application/rss+xml" />
	<link>http://query7.com</link>
	<description>PHP, Javascript, Python and Web Development</description>
	<lastBuildDate>Sat, 25 Jun 2011 21:29:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Creating an inline contact form</title>
		<link>http://query7.com/creating-an-inline-contact-form</link>
		<comments>http://query7.com/creating-an-inline-contact-form#comments</comments>
		<pubDate>Mon, 07 Sep 2009 10:41:50 +0000</pubDate>
		<dc:creator>Kishore Nallan</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.webdevelopmentbits.com/?p=507</guid>
		<description><![CDATA[<p>Customer feedback is arguably one of the most critical factors determining the success of a product over a period of time. Hence, it&#8217;s not surprising that many websites have some form of contact form or another to encourage users to write back to them. Most of these &#8220;contact&#8221; pages tend to be on a separate page which is usually part of the navigation or is linked to with phrases like &#8220;we encourage your feedback&#8221; or &#8220;do get in touch with us&#8221;, etc.</p>
<p>However, I have often noticed that many people (unless they genuinely needed help), either back away from submitting their  comments (when they are confronted by a large  textarea on the contact page), or simply move away from the site as the contact page loads.</p>
<p>Today, let&#8217;s spice things up a bit by using jQuery to load a lightweight inline contact form. So, instead of loading a separate contact form page when the &#8220;contact us&#8221; link is clicked, we will just have a small textarea opens up dynamically right next to  the link &#8211; so that the user can type his feedback and it can be  submitted rightaway using AJAX. Our main aim here is to encourage more users&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Customer feedback is arguably one of the most critical factors determining the success of a product over a period of time. Hence, it&#8217;s not surprising that many websites have some form of contact form or another to encourage users to write back to them. Most of these &#8220;contact&#8221; pages tend to be on a separate page which is usually part of the navigation or is linked to with phrases like &#8220;we encourage your feedback&#8221; or &#8220;do get in touch with us&#8221;, etc.</p>
<p>However, I have often noticed that many people (unless they genuinely needed help), either back away from submitting their  comments (when they are confronted by a large  textarea on the contact page), or simply move away from the site as the contact page loads.</p>
<p>Today, let&#8217;s spice things up a bit by using jQuery to load a lightweight inline contact form. So, instead of loading a separate contact form page when the &#8220;contact us&#8221; link is clicked, we will just have a small textarea opens up dynamically right next to  the link &#8211; so that the user can type his feedback and it can be  submitted rightaway using AJAX. Our main aim here is to encourage more users to provide feedback and although I don&#8217;t have actual data to prove whether an inline form will be any more helpful, I have used it in a  couple of places, and it did appear to bring in more casual feedback  about the site, features etc. Ok, let&#8217;s get started. Here is the <strong><a href="http://www.webdevelopmentbits.com/temp/inline_form_demo.html">demo</a></strong> of how it will all look in the end.</p>
<p>Okay, first let&#8217;s have a hypothetical page in which we want to make use of the inline contact form. The call of action, is obviously the &#8220;We would love to hear your feedback&#8221; link, which has the id &#8220;a_inline_form&#8221;. Initially, this hyperlink will simply point to the default contact form page. We will have the inline form in a DIV with id &#8220;inline_form&#8221;, which will be hidden initially.</p>
<pre>#inline_form {
		/* keep the inline form hidden initially */
		display: none;

		position: relative;
		padding: 10px;
		width: 260px;
	}</pre>
<p>We will now use jQuery to &#8220;hijack&#8221; this link and fire our inline contact form into action:</p>
<pre> // toggle visibility of inline form when link is clicked
$("#a_inline_form").click( function() {

	$("#inline_form").toggle(200);

	$("#inline_form").css( "left", $("#a_inline_form").position().left +
                                                $("#a_inline_form").width() );

	$("#inline_form").css("top", "-40px");

	// get cursor to texarea, ready to receive keystrokes!
	$("#inlinecmt").focus();

	// return false to prevent default link behaviour
	return false;
});</pre>
<p>The above code is fairly self explanatory &#8211; I just want to describe how we are positioning the inline form. jQuery allows us to very easily retrieve the left offset position of an element (even when it&#8217;s not absolutely or relatively positioned!) using &#8220;$(elem).position().left&#8221; property.</p>
<p>In this example, I wanted to place the inline form right next to the comment link. To do that, we can simply set the &#8220;left&#8221; CSS property of the inline form to a value that&#8217;s the sum of the left offset of the feedback link and the width of the comment link &#8211; this will of course place the inline form right next to the feedback link. In addition, we also set the cursor focus to the inline form so that the textarea is ready to receive input right away.</p>
<p>We have a simple &#8220;ok&#8221; button underneath the textarea which will be used to submit the form. We will once again &#8220;hijack&#8221; the form submit event using jQuery and perform the form submission using AJAX. I have omitted the actual form submission and processing here.</p>
<pre>$("#inline_form").submit( function() {

	// make your ajax POST call here...

	$("#a_inline_form").after('&lt;span class="msg"&gt;Thanks for the comment!' +
                               '&lt;/span&gt;');
	$("#inline_form").hide(200);

	setTimeout( '$(".msg").hide()', 2000);

	// return false to prevent default form submission behavior
	return false;
});</pre>
<p>Now, once the AJAX call is successful, we need to notify that to the user and thank him for the feedback. I have chosen to do that by inserting a message right after the feedback link and hiding it (using setTimeout() ) after 2 seconds. Don&#8217;t forget to hide the inline form too. With that, our inline form is up and ready to go!</p>
<p>This is by no means a replacement for the traditional contact form, but it will be handy in places where one requires casual feedback from users. And as you would have already noticed, we have used JavaScript here unobtrusively &#8211; so in case the user has JavaScript disabled, the feedback link merely loads the <a href="http://www.webdevelopmentbits.com/temp/inline_form_contact.html">default contact page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://query7.com/creating-an-inline-contact-form/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

