<?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>jensbits.com &#187; forms</title>
	<atom:link href="http://www.jensbits.com/tag/forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jensbits.com</link>
	<description></description>
	<lastBuildDate>Wed, 21 Jul 2010 03:44:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>jQuery UI Autocomplete Widget with ASP.NET VB</title>
		<link>http://www.jensbits.com/2010/04/14/jquery-ui-autocomplete-widget-with-asp-net/</link>
		<comments>http://www.jensbits.com/2010/04/14/jquery-ui-autocomplete-widget-with-asp-net/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 01:00:23 +0000</pubDate>
		<dc:creator>jen</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[forms]]></category>

		<guid isPermaLink="false">http://www.jensbits.com/?p=671</guid>
		<description><![CDATA[You might also be interested in the Using jQuery Autocomplete to Populate Another Autocomplete post. As a follow up to the jQuery UI Autocomplete Widget with ColdFusion and the jQuery UI Autocomplete Widget with PHP posts, I did one with ASP.NET (VB.NET) as the backend. I swear this is the last one I&#8217;m doing. I&#8217;m [...]


Related posts:<ol><li><a href='http://www.jensbits.com/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/' rel='bookmark' title='Permanent Link: Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples'>Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples</a></li>
<li><a href='http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/' rel='bookmark' title='Permanent Link: jQuery UI Autocomplete Widget with PHP and MySQL'>jQuery UI Autocomplete Widget with PHP and MySQL</a></li>
<li><a href='http://www.jensbits.com/2010/03/18/jquery-ui-autocomplete-with-coldfusion/' rel='bookmark' title='Permanent Link: jQuery UI Autocomplete Widget with ColdFusion'>jQuery UI Autocomplete Widget with ColdFusion</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote><p>You might also be interested in the <a href="/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/">Using jQuery Autocomplete to Populate Another Autocomplete post</a>.</p></blockquote>
<p>As a follow up to the<a href="/2010/03/18/jquery-ui-autocomplete-with-coldfusion/"> jQuery UI Autocomplete Widget with ColdFusion</a> and the  <a href="2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/">jQuery UI Autocomplete Widget with PHP</a>  posts, I did one with ASP.NET (VB.NET) as the backend. I swear this is  the last one I&#8217;m doing. I&#8217;m running out of languages that I work in.</p>
<p>The jQuery UI folks have released an <a href=" http://jqueryui.com/demos/autocomplete/">autocomplete widget</a> that is pretty slick. This example uses the JavaScriptSerializer() function in .NET 3.5. I heard a rumor .NET 4 might make this json encoding with data easier. We&#8217;ll see.<br />
<img src="/images/autocomplete_asp.gif" alt="autocomplete" /><br />
This example will use US states and territories to populate the autocomplete. It will also demonstrate how to fill other fields with data returned from the database. This data can be used to fill a visible text box or a hidden form field. It also demonstrates the basic autocomplete functionality which may be fine for some applications.</p>
<p>Of course, you will need the jQuery core file, the jQuery UI core file, and the jQuery UI style sheet of choice. The style sheet comes from the themes available in the jQuery UI website and can be <a href="http://jqueryui.com/download">downloaded with the core file</a>:</p>
<pre class="brush: xml;">
&lt;link type=&quot;text/css&quot; href=&quot;jquery-ui-1.8rc3.custom.css&quot; rel=&quot;stylesheet&quot; /&gt; 

&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.4.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-ui-1.8rc3.custom.min.js&quot;&gt;&lt;/script&gt;
</pre>
<p>The HTML is straight forward and stripped down for the example:</p>
<pre class="brush: xml;">
&lt;form action=&quot;Default.aspx&quot;  method=&quot;post&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;jQuery UI Autocomplete Example - ASP.NET VB Backend&lt;/legend&gt;
&lt;p&gt;Start typing the name of a state or territory of the United States&lt;/p&gt;
&lt;p class=&quot;ui-widget&quot;&gt;&lt;label for=&quot;state&quot;&gt;State (abbreviation in separate field): &lt;/label&gt;
	&lt;input type=&quot;text&quot; id=&quot;state&quot;  name=&quot;state&quot; /&gt; &lt;input readonly=&quot;readonly&quot; type=&quot;text&quot; id=&quot;abbrev&quot; name=&quot;abbrev&quot; maxlength=&quot;2&quot; size=&quot;2&quot;/&gt;&lt;/p&gt;
    &lt;input type=&quot;hidden&quot; id=&quot;state_id&quot; name=&quot;state_id&quot; /&gt;
&lt;p class=&quot;ui-widget&quot;&gt;&lt;label for=&quot;state_abbrev&quot;&gt;State (replaced with abbreviation): &lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;state_abbrev&quot; name=&quot;state_abbrev&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
</pre>
<p>As a bonus, we dump out the form values to see what we have right underneath the form itself:</p>
<pre class="brush: vb;">
Sub Page_Load(Source As Object, E As EventArgs)
 	Dim formfields As String = &quot;&lt;p&gt;&quot; 

     For Each sItem In Request.Form
	 	formfields = formfields + &quot;&lt;strong&gt;&quot; + sItem + &quot;&lt;/strong&gt; = &quot; +  Request.Form(sItem) + &quot;&lt;br /&gt;&quot;
  	Next
	formoutput.Text = formfields + &quot;&lt;/p&gt;&quot;
 End Sub
</pre>
<p>And the jQuery on the page is equally brief:</p>
<pre class="brush: jscript;">
$(function() {

            $('#abbrev').val(&quot;&quot;);

            $(&quot;#state&quot;).autocomplete({
                source: &quot;states.php&quot;,
                minLength: 2,
                select: function(event, ui) {
                    $('#state_id').val(ui.item.id);
                    $('#abbrev').val(ui.item.abbrev);
                }
            });

            $(&quot;#state_abbrev&quot;).autocomplete({
                source: &quot;states_abbrev.php&quot;,
                minLength: 2
            });
        });
</pre>
<p>Notice that there are two autocomplete functions on the page, one for each example in the demo. Each function calls a different aspx file which return slightly different result sets.</p>
<p>Also, the minLength for autocomplete to return results is set to 2 to prevent too many rows from being returned.</p>
<p>Both .NET pages return the data after a few steps:</p>
<ol>
<li>It creates a new javascript serializer</li>
<li>It creates an object to hold the data from each returned row in the query</li>
<li>It queries the database and fills a dataset (keep reading if you like readers better)</li>
<li>Loops an array of the query results adding each row to an object</li>
<li>Adds the object to an ArrayList</li>
<li>Outputs the ArrayList as JSON data</li>
</ol>
<p>The states.aspx file returns the id field, the state field as &#8216;value&#8217;, and the abbrev field. These values are placed in the appropriate text boxes by the autocomplete jQuery function. </p>
<pre class="brush: vb;">
&lt;%@ Page Language=&quot;VB&quot; Debug=&quot;false&quot; %&gt;
&lt;%@ Import Namespace=&quot;System.Web.Script.Serialization&quot; %&gt;
&lt;%@ Import Namespace=&quot;System.Data&quot; %&gt;
&lt;%@ Import Namespace=&quot;System.Data.SqlClient&quot; %&gt;

&lt;script runat=&quot;server&quot;&gt;
    Dim serializer As JavaScriptSerializer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        serializer = New JavaScriptSerializer()
        Response.Write(JSONData(Request.QueryString(&quot;Term&quot;)))
    End Sub

    Public Class State
        Public id As Integer
        Public value As String
        Public abbrev As String
    End Class

    Private Function JSONData(ByVal term As String) As String

        Dim stateArray As New ArrayList
        Dim index As Integer = 0

        Dim mySql As String
        Dim objConn As New SqlConnection(&quot;YOUR-CONNECTION-STRING-HERE&quot;)
        Dim myds As New DataSet(&quot;States&quot;)
        mySql = &quot;SELECT id, state, abbrev FROM states WHERE state like '%&quot; + term + &quot;%'&quot;

        objConn.Open()

        Dim adapter As New SqlClient.SqlDataAdapter(mySql, objConn)
        adapter.Fill(myds, &quot;States&quot;)
        For Each dr As DataRow In myds.Tables(0).Rows
            Dim st As New State()
            st.id = dr(&quot;id&quot;).ToString()
            st.value = dr(&quot;state&quot;).ToString()
            st.abbrev = dr(&quot;abbrev&quot;).ToString()
            stateArray.Add(st)
        Next

        objConn.Close()

        Return serializer.Serialize(stateArray)
    End Function

    &lt;/script&gt;
</pre>
<p>If you prefer to use a reader, just substitute the code below for the dataset code above.</p>
<pre class="brush: vb;">
        Dim command As New SqlCommand(mySql, objConn)
        Dim reader As SqlDataReader = command.ExecuteReader()

        While reader.Read()
            Dim st As New State()
            st.id = reader(&quot;id&quot;).ToString()
            st.value = reader(&quot;state&quot;).ToString()
            st.abbrev = reader(&quot;abbrev&quot;).ToString()
            stateArray.Add(st)
        End While

        reader.Close()
</pre>
<p>The states_abbrev.aspx shows the basic functionality of the autocomplete function by just assigning results of the query to the &#8216;label&#8217; and &#8216;value&#8217; fields. Explanation on the &#8216;label&#8217; and &#8216;value&#8217; fields from the jQuery UI site:</p>
<p><em>&#8220;The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.&#8221;</em></p>
<pre class="brush: vb;">
&lt;%@ Page Language=&quot;VB&quot; Debug=&quot;false&quot; %&gt;
&lt;%@ Import Namespace=&quot;System.Web.Script.Serialization&quot; %&gt;
&lt;%@ Import Namespace=&quot;System.Data&quot; %&gt;
&lt;%@ Import Namespace=&quot;System.Data.SqlClient&quot; %&gt;

&lt;script runat=&quot;server&quot;&gt;
    Dim serializer As JavaScriptSerializer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        serializer = New JavaScriptSerializer()
        Response.Write(JSONData(Request.QueryString(&quot;Term&quot;)))
    End Sub

    Public Class State
        Public label As String
        Public value As String
    End Class

    Private Function JSONData(ByVal term As String) As String

        Dim stateArray As New ArrayList
        Dim index As Integer = 0

        Dim mySql As String
        Dim objConn As New SqlConnection(&quot;YOUR-CONNECTION-STRING-HERE&quot;)
        Dim myds As New DataSet(&quot;States&quot;)
        mySql = &quot;SELECT id, state, abbrev FROM states WHERE state like '%&quot; + term + &quot;%'&quot;

        objConn.Open()

        Dim adapter As New SqlClient.SqlDataAdapter(mySql, objConn)
        adapter.Fill(myds, &quot;States&quot;)
        For Each dr As DataRow In myds.Tables(0).Rows
            Dim st As New State()
            st.label = dr(&quot;state&quot;).ToString()
            st.value = dr(&quot;abbrev&quot;).ToString()
            stateArray.Add(st)
        Next

		objConn.Close()

        Return serializer.Serialize(stateArray)
    End Function

    &lt;/script&gt;
</pre>
<p>Again, if you prefer to use a reader, here you go:</p>
<pre class="brush: vb;">
       Dim command As New SqlCommand(mySql, objConn)
        Dim reader As SqlDataReader = command.ExecuteReader()

        While reader.Read()
            Dim st As New State()
            st.label = reader(&quot;state&quot;).ToString()
            st.state = reader(&quot;abbrev&quot;).ToString()
            stateArray.Add(st)
        End While

        reader.Close()
</pre>
<p>Usual recommended jQuery and .NET reading:</p>
<div style="height: 250px;">
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0596159773" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0321647491" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0980576857" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe><br />
</iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0672330113" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe><br />
</iframe></p>
</div>
</div>
<p id="demo"><a href="http://cf-jensbits.com/demos/autocomplete_asp/" onclick="_gaq.push(['_link', 'http://cf-jensbits.com/demos/autocomplete_asp/']); return false;"><span>Demo</span></a></p>
<p class="donate">If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.</p>

<!-- Begin PayPal Donations by http://wpstorm.net/ -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="jen@jensbits.com" /><input type="hidden" name="return" value="http://www.jensbits.com/thank-you/" /><input type="hidden" name="item_name" value="Help pay hosting. All donations go to hosting fees for this site." /><input type="hidden" name="currency_code" value="USD" /><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
<!-- End PayPal Donations -->



<p>Related posts:<ol><li><a href='http://www.jensbits.com/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/' rel='bookmark' title='Permanent Link: Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples'>Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples</a></li>
<li><a href='http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/' rel='bookmark' title='Permanent Link: jQuery UI Autocomplete Widget with PHP and MySQL'>jQuery UI Autocomplete Widget with PHP and MySQL</a></li>
<li><a href='http://www.jensbits.com/2010/03/18/jquery-ui-autocomplete-with-coldfusion/' rel='bookmark' title='Permanent Link: jQuery UI Autocomplete Widget with ColdFusion'>jQuery UI Autocomplete Widget with ColdFusion</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.jensbits.com/2010/04/14/jquery-ui-autocomplete-widget-with-asp-net/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion</title>
		<link>http://www.jensbits.com/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/</link>
		<comments>http://www.jensbits.com/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 13:17:30 +0000</pubDate>
		<dc:creator>jen</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[forms]]></category>

		<guid isPermaLink="false">http://www.jensbits.com/?p=441</guid>
		<description><![CDATA[This is the same as the previous jquery form submit post that used PHP just flavored with CF this time. Two jQuery functions that allow for the submission of form are the jQuery.ajax function and the jQuery.post function (there is also jQuery.get but that is not addressed here). More functionality, along with more complexity, is [...]


Related posts:<ol><li><a href='http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with PHP'>jQuery.ajax and jQuery.post Form Submit Examples with PHP</a></li>
<li><a href='http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/' rel='bookmark' title='Permanent Link: Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties'>Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties</a></li>
<li><a href='http://www.jensbits.com/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/' rel='bookmark' title='Permanent Link: Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples'>Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote><p>This is the same as the previous <a href="/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/">jquery form submit post that used PHP</a> just flavored with CF this time.</p></blockquote>
<p>Two jQuery functions that allow for the submission of form are the jQuery.ajax function and the jQuery.post function (there is also jQuery.get but that is not addressed here). </p>
<p>More functionality, along with more complexity, is offered with the .ajax function while the .post function, with its more simple functionality and implementation, will be all that is needed for simple form posts.</p>
<blockquote><p>It is <strong>highly recommended</strong> that you get a tool like <a href="http://getfirebug.com/">Firebug</a> to see the post response coming back from the page. It helps immensely.</p></blockquote>
<p>Here is an example of both in action doing the same thing: form submit with email validation.</p>
<h2>jQuery.ajax</h2>
<p> The form:</p>
<pre class="brush: xml;">
&lt;form id=&quot;JqAjaxForm&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;jQuery.ajax Form Submit&lt;/legend&gt;
&lt;p&gt;&lt;label for=&quot;name_ajax&quot;&gt;Name:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;name_ajax&quot; type=&quot;text&quot; name=&quot;name_ajax&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;email_ajax&quot;&gt;E-mail:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;email_ajax&quot; type=&quot;text&quot; name=&quot;email_ajax&quot;  /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;div id=&quot;message_ajax&quot;&gt;&lt;/div&gt;
</pre>
<p>Pretty simple, nothing fancy. There is a form for each jQuery function, .ajax and .post. The only difference in the forms are the element names.</p>
<p>jQuery controls the submit for the forms. For the .ajax submission the jQuery is this:</p>
<pre class="brush: jscript;">
$(function(){
    $(&quot;#JqAjaxForm&quot;).submit(function(){
        dataString = $(&quot;#JqAjaxForm&quot;).serialize();

        $.ajax({
        type: &quot;POST&quot;,
        url: &quot;process_form.cfm&quot;,
        data: dataString,
        dataType: &quot;json&quot;,
        success: function(data) {

            if(data.email_check == &quot;invalid&quot;){
                $(&quot;#message_ajax&quot;).html(&quot;&lt;div class='errorMessage'&gt;Sorry &quot; + data.name + &quot;, &quot; + data.email + &quot; is NOT a valid e-mail address. Try again.&lt;/div&gt;&quot;);
            } else {
                $(&quot;#message_ajax&quot;).html(&quot;&lt;div class='successMessage'&gt;&quot; + data.email + &quot; is a valid e-mail address. Thank you, &quot; + data.name + &quot;.&lt;/div&gt;&quot;);
            }

        }

        });

        return false;            

    });
});
</pre>
<p>The .serialize function is used to put the form data in a format that can be processed by a page on the server. The .ajax function options include:</p>
<ul>
<li>type: &#8220;get&#8221; or &#8220;post&#8221;</li>
<li>url: the page to receive the form data</li>
<li>data: the form data itself</li>
<li>dataType: the data type the function should expect back from the server</li>
<li>success function: runs on a succesful post to the page</li>
</ul>
<p>More information on the options and further explanations of the options used here can be found on the<a href="http://docs.jquery.com/Ajax/jQuery.ajax"> jQuery.ajax documentation page</a>.</p>
<h2>jQuery.post</h2>
<p>As in the .ajax example, the form is simple, only the names have been changed:</p>
<pre class="brush: xml;">
&lt;form id=&quot;JqPostForm&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;jQuery.post Form Submit&lt;/legend&gt;
&lt;p&gt;&lt;label for=&quot;name_post&quot;&gt;Name:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;name_post&quot; type=&quot;text&quot; name=&quot;name_post&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;email_post&quot;&gt;E-mail:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;email_post&quot; type=&quot;text&quot; name=&quot;email_post&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;div id=&quot;message_post&quot;&gt;&lt;/div&gt;
</pre>
<p>And again, jQuery controls the submit for the forms. For the .post submission the jQuery is this:</p>
<pre class="brush: jscript;">
$(function(){
    $(&quot;#JqPostForm&quot;).submit(function(){
        $.post(&quot;process_form.cfm&quot;, $(&quot;#JqPostForm&quot;).serialize(),
        function(data){
            if(data.email_check == 'invalid'){

                    $(&quot;#message_post&quot;).html(&quot;&lt;div class='errorMessage'&gt;Sorry &quot; + data.name + &quot;, &quot; + data.email + &quot; is NOT a valid e-mail address. Try again.&lt;/div&gt;&quot;);
            } else {
                $(&quot;#message_post&quot;).html(&quot;&lt;div class='successMessage'&gt;&quot; + data.email + &quot; is a valid e-mail address. Thank you, &quot; + data.name + &quot;.&lt;/div&gt;&quot;);
                }
        }, &quot;json&quot;);

        return false;

    });
});
</pre>
<p>jQuery.post is a shorter, easier way to post the form data. The function arguments are:</p>
<ul>
<li>url of the form processing page</li>
<li>the form data</li>
<li>the callback function
<li>
<li>the data type of the return data</li>
</ul>
<p>More information can be found on the<a href="http://docs.jquery.com/Ajax/jQuery.post"> jQuery.post documentation page</a>.</p>
<h2>Processing the Form</h2>
<p>Both methods are processed by the same page. It processes the form data, process_form.cfm in this example, by checking to see if the e-mail submitted is valid. Much more than that could be done on the page if needed.</p>
<pre class="brush: coldfusion;">
&lt;cfset email_check = &quot;&quot; /&gt;
&lt;cfset return_json_string = &quot;&quot; /&gt;
&lt;cfset return_struct = StructNew() /&gt;

&lt;cfif (isDefined(&quot;form.email_ajax&quot;) AND isValid(&quot;email&quot;,form.email_ajax)) OR (isDefined(&quot;form.email_post&quot;) AND isValid(&quot;email&quot;,form.email_post))&gt;
   &lt;cfset email_check = &quot;valid&quot;/&gt;
&lt;cfelse&gt;
    &lt;cfset email_check = &quot;invalid&quot;/&gt;
&lt;/cfif&gt;

&lt;cfset StructInsert(return_struct, &quot;email_check&quot;, email_check) /&gt;

&lt;cfif isDefined(&quot;form.email_ajax&quot;)&gt;
	&lt;cfset StructInsert(return_struct, &quot;name&quot;, form.name_ajax) /&gt;
	&lt;cfset StructInsert(return_struct, &quot;email&quot;, form.email_ajax) /&gt;

	&lt;!---return_json_string is for pre-CF 8 only, delete if you have cf 8 or later---&gt;
	&lt;cfset return_json_string = '{&quot;email_check&quot;:&quot;#email_check#&quot;,&quot;name&quot;:&quot;' &amp; form.name_ajax &amp; '&quot;,&quot;email&quot;:&quot;' &amp; form.email_ajax &amp; '&quot;}' /&gt;

&lt;cfelse&gt;
	&lt;cfset StructInsert(return_struct, &quot;name&quot;, form.name_post) /&gt;
	&lt;cfset StructInsert(return_struct, &quot;email&quot;, form.email_post) /&gt;

	&lt;!---return_json_string is for pre-CF 8 only, delete if you have cf 8 or later---&gt;
	&lt;cfset return_json_string = '{&quot;email_check&quot;:&quot;#email_check#&quot;,&quot;name&quot;:&quot;' &amp; form.name_post &amp; '&quot;,&quot;email&quot;:&quot;' &amp; form.email_post &amp; '&quot;}' /&gt;

&lt;/cfif&gt;

&lt;!--- serializeJSON is CF 8 and above only, see below for pre-CF 8 ---&gt;
&lt;cfoutput&gt;#serializeJSON(return_struct)#&lt;/cfoutput&gt;

&lt;!--- Uncomment the cfoutput statement below and remove the cfoutput statement above if you don't have CF 8---&gt;
&lt;!--- &lt;cfoutput&gt;#return_json_string#&lt;/cfoutput&gt; ---&gt;
</pre>
<p>This code puts the results of the e-mail validation and the form data in a <a href="http://json.org/">JSON-formatted</a> string. It then will output the return data string which is picked up by the success function in the .ajax function or the function(data) function in the .post function on the original page. </p>
<p>I prefer working with JSON, but there are other options for the return data. Check the jQuery documentation for the types available to you.</p>
<p>The JSON response will look like this:</p>
<pre class="brush: jscript;">
{&quot;email_check&quot;:&quot;valid&quot;,&quot;name&quot;:&quot;Julia&quot;,&quot;email&quot;:&quot;julia@example.com&quot;}
</pre>
<p><span style="color: red;font-weight: bold;">NOTE: If you have ColdFusion 8 or better use the <a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_s_03.html">serializeJSON function</a>. This function can return query results, arrays, dates, strings, and the like to JSON which is easily consumed and digested by javascript. Pre-CF 8 will require building the JSON string manually but with some loops and other creativity, it can be done.</span></p>
<p>The appropriate message based on the e-mail validation check is then displayed.</p>
<p>Pretty simple, pretty handy couple of jQuery functions. Once you see it in action, you get the idea.</p>
<p>Usual recommended jQuery and CF reading:</p>
<div style="height: 250px;">
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0596159773" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0321647491" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=1847195121" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=032151548X" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
</div>
<p id="demo"><a href="http://cf-jensbits.com/demos/jquery/submit/" onclick="_gaq.push(['_link', 'http://cf-jensbits.com/demos/jquery/submit/']); return false;"><span>Demo</span></a></p>
<p id="download"><a href="/media/code/jQAjaxPostCF.zip"><span>Download zip of all files</span></a></p>
<p class="donate">If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.</p>

<!-- Begin PayPal Donations by http://wpstorm.net/ -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="jen@jensbits.com" /><input type="hidden" name="return" value="http://www.jensbits.com/thank-you/" /><input type="hidden" name="item_name" value="Help pay hosting. All donations go to hosting fees for this site." /><input type="hidden" name="currency_code" value="USD" /><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
<!-- End PayPal Donations -->



<p>Related posts:<ol><li><a href='http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with PHP'>jQuery.ajax and jQuery.post Form Submit Examples with PHP</a></li>
<li><a href='http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/' rel='bookmark' title='Permanent Link: Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties'>Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties</a></li>
<li><a href='http://www.jensbits.com/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/' rel='bookmark' title='Permanent Link: Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples'>Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.jensbits.com/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>jQuery.ajax and jQuery.post Form Submit Examples with PHP</title>
		<link>http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/</link>
		<comments>http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 00:11:55 +0000</pubDate>
		<dc:creator>jen</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[forms]]></category>

		<guid isPermaLink="false">http://www.jensbits.com/?p=425</guid>
		<description><![CDATA[For a version of this using ColdFusion, see the CF jquery form submit post. Two jQuery functions that allow for the submission of form are the jQuery.ajax function and the jQuery.post function (there is also jQuery.get but that is not addressed here). More functionality, along with more complexity, is offered with the .ajax function while [...]


Related posts:<ol><li><a href='http://www.jensbits.com/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion'>jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion</a></li>
<li><a href='http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/' rel='bookmark' title='Permanent Link: Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties'>Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties</a></li>
<li><a href='http://www.jensbits.com/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/' rel='bookmark' title='Permanent Link: Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples'>Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote><p>For a version of this using ColdFusion, see the <a href="/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/">CF jquery form submit post</a>.</p></blockquote>
<p>Two jQuery functions that allow for the submission of form are the jQuery.ajax function and the jQuery.post function (there is also jQuery.get but that is not addressed here). </p>
<p>More functionality, along with more complexity, is offered with the .ajax function while the .post function, with its more simple functionality and implementation, will be all that is needed for simple form posts.</p>
<blockquote><p>It is <strong>highly recommended</strong> that you get a tool like <a href="http://getfirebug.com/">Firebug</a> to see the post response coming back from the page. It helps immensely.</p></blockquote>
<p>Here is an example of both in action doing the same thing: form submit with email validation.</p>
<h2>jQuery.ajax</h2>
<p> The form:</p>
<pre class="brush: xml;">
&lt;form id=&quot;JqAjaxForm&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;jQuery.ajax Form Submit&lt;/legend&gt;
&lt;p&gt;&lt;label for=&quot;name_ajax&quot;&gt;Name:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;name_ajax&quot; type=&quot;text&quot; name=&quot;name_ajax&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;email_ajax&quot;&gt;E-mail:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;email_ajax&quot; type=&quot;text&quot; name=&quot;email_ajax&quot;  /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;div id=&quot;message_ajax&quot;&gt;&lt;/div&gt;
</pre>
<p>Pretty simple, nothing fancy. There is a form for each jQuery function, .ajax and .post. The only difference in the forms are the element names.</p>
<p>jQuery controls the submit for the forms. For the .ajax submission the jQuery is this:</p>
<pre class="brush: jscript;">
$(function(){
    $(&quot;#JqAjaxForm&quot;).submit(function(){
        dataString = $(&quot;#JqAjaxForm&quot;).serialize();

        $.ajax({
        type: &quot;POST&quot;,
        url: &quot;process_form.php&quot;,
        data: dataString,
        dataType: &quot;json&quot;,
        success: function(data) {

            if(data.email_check == &quot;invalid&quot;){
                $(&quot;#message_ajax&quot;).html(&quot;&lt;div class='errorMessage'&gt;Sorry &quot; + data.name + &quot;, &quot; + data.email + &quot; is NOT a valid e-mail address. Try again.&lt;/div&gt;&quot;);
            } else {
                $(&quot;#message_ajax&quot;).html(&quot;&lt;div class='successMessage'&gt;&quot; + data.email + &quot; is a valid e-mail address. Thank you, &quot; + data.name + &quot;.&lt;/div&gt;&quot;);
            }

        }

        });

        return false;            

    });
});
</pre>
<p>The .serialize function is used to put the form data in a format that can be processed by a page on the server. The .ajax function options include:</p>
<ul>
<li>type: &#8220;get&#8221; or &#8220;post&#8221;</li>
<li>url: the page to receive the form data</li>
<li>data: the form data itself</li>
<li>dataType: the data type the function should expect back from the server</li>
<li>success function: runs on a succesful post to the page</li>
</ul>
<p>More information on the options and further explanations of the options used here can be found on the<a href="http://docs.jquery.com/Ajax/jQuery.ajax"> jQuery.ajax documentation page</a>.</p>
<h2>jQuery.post</h2>
<p>As in the .ajax example, the form is simple, only the names have been changed:</p>
<pre class="brush: xml;">
&lt;form id=&quot;JqPostForm&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;jQuery.post Form Submit&lt;/legend&gt;
&lt;p&gt;&lt;label for=&quot;name_post&quot;&gt;Name:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;name_post&quot; type=&quot;text&quot; name=&quot;name_post&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;email_post&quot;&gt;E-mail:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;email_post&quot; type=&quot;text&quot; name=&quot;email_post&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;div id=&quot;message_post&quot;&gt;&lt;/div&gt;
</pre>
<p>And again, jQuery controls the submit for the forms. For the .post submission the jQuery is this:</p>
<pre class="brush: jscript;">
$(function(){
    $(&quot;#JqPostForm&quot;).submit(function(){
        $.post(&quot;process_form.php&quot;, $(&quot;#JqPostForm&quot;).serialize(),
        function(data){
            if(data.email_check == 'invalid'){

                    $(&quot;#message_post&quot;).html(&quot;&lt;div class='errorMessage'&gt;Sorry &quot; + data.name + &quot;, &quot; + data.email + &quot; is NOT a valid e-mail address. Try again.&lt;/div&gt;&quot;);
            } else {
                $(&quot;#message_post&quot;).html(&quot;&lt;div class='successMessage'&gt;&quot; + data.email + &quot; is a valid e-mail address. Thank you, &quot; + data.name + &quot;.&lt;/div&gt;&quot;);
                }
        }, &quot;json&quot;);

        return false;

    });
});
</pre>
<p>jQuery.post is a shorter, easier way to post the form data. The function arguments are:</p>
<ul>
<li>url of the form processing page</li>
<li>the form data</li>
<li>the callback function
<li>
<li>the data type of the return data</li>
</ul>
<p>More information can be found on the<a href="http://docs.jquery.com/Ajax/jQuery.post"> jQuery.post documentation page</a>.</p>
<h2>Processing the Form</h2>
<p>Both methods are processed by the same page. It processes the form data, process_form.php in this example, by checking to see if the e-mail submitted is valid. Much more than that could be done on the page if needed.</p>
<pre class="brush: php;">
$email_check = '';
$return_json = '';

function isValidEmail($email){
    return eregi(&quot;^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$&quot;, $email);
}

if(isValidEmail($_POST['email_ajax']) || isValidEmail($_POST['email_post'])) {
   $email_check = 'valid';
}
else {
    $email_check = 'invalid';
}

$return_json = '{&quot;email_check&quot;:&quot;' . $email_check . '&quot;,';

if (isset($_POST['email_ajax'])){
    $return_json = $return_json . '&quot;name&quot;:&quot;' . $_POST['name_ajax'] . '&quot;,';
    $return_json = $return_json . '&quot;email&quot;:&quot;' . $_POST['email_ajax'] . '&quot;}';
} else {
    $return_json = $return_json . '&quot;name&quot;:&quot;' . $_POST['name_post'] . '&quot;,';
    $return_json = $return_json . '&quot;email&quot;:&quot;' . $_POST['email_post'] . '&quot;}';
}

echo $return_json;
</pre>
<p>This code puts the results of the e-mail validation and the form data in a <a href="http://json.org/">JSON-formatted</a> string. It then will echo the return data string which is picked up by the success function in the .ajax function or the function(data) function in the .post function on the original page. </p>
<p>I prefer working with JSON, but there are other options for the return data. Check the jQuery documentation for the types available to you.</p>
<p>The JSON response will look like this:</p>
<pre class="brush: jscript;">
{&quot;email_check&quot;:&quot;valid&quot;,&quot;name&quot;:&quot;Julia&quot;,&quot;email&quot;:&quot;julia@example.com&quot;}
</pre>
<p><span style="color: red;font-weight: bold;">NOTE: If you have PHP 5.2 or better and the <a href="http://us3.php.net/filter_var">filter_var</a> and <a href="http://us3.php.net/json_encode">json_encode</a> functions available, use the following code instead:</span></p>
<pre class="brush: php;">
$email_check = '';
$return_arr = array();

if(filter_var($_POST['email_ajax'], FILTER_VALIDATE_EMAIL) || filter_var($_POST['email_post'], FILTER_VALIDATE_EMAIL)) {
   $email_check = 'valid';
}
else {
    $email_check = 'invalid';
}

$return_arr[&quot;email_check&quot;] = $email_check;

if (isset($_POST['email_ajax'])){
    $return_arr[&quot;name&quot;] = $_POST['name_ajax'];
    $return_arr[&quot;email&quot;] = $_POST['email_ajax'];
} else {
    $return_arr[&quot;name&quot;] = $_POST['name_post'];
    $return_arr[&quot;email&quot;] = $_POST['email_post'];

}

echo json_encode($return_arr);
</pre>
<p>The appropriate message based on the e-mail validation check is then displayed.</p>
<p>Pretty simple, pretty handy couple of jQuery functions. Once you see it in action, you get the idea.</p>
<p>Usual recommended jQuery and PHP reading:</p>
<div style="height: 250px;">
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=1847196705" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0321647491" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=1847195121" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0764557467" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
</div>
<p id="demo"><a href="/demos/jqsubmit/index.php"><span>Demo</span></a></p>
<p id="download"><a href="/media/code/jQAjaxPostPHP.zip"><span>Download zip of all files</span></a></p>
<p class="donate">If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.</p>

<!-- Begin PayPal Donations by http://wpstorm.net/ -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="jen@jensbits.com" /><input type="hidden" name="return" value="http://www.jensbits.com/thank-you/" /><input type="hidden" name="item_name" value="Help pay hosting. All donations go to hosting fees for this site." /><input type="hidden" name="currency_code" value="USD" /><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
<!-- End PayPal Donations -->



<p>Related posts:<ol><li><a href='http://www.jensbits.com/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion'>jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion</a></li>
<li><a href='http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/' rel='bookmark' title='Permanent Link: Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties'>Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties</a></li>
<li><a href='http://www.jensbits.com/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/' rel='bookmark' title='Permanent Link: Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples'>Using jQuery Autocomplete to Populate Another Autocomplete &#8211; ASP.NET, ColdFusion, and PHP Examples</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties</title>
		<link>http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/</link>
		<comments>http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 13:15:15 +0000</pubDate>
		<dc:creator>jen</dc:creator>
				<category><![CDATA[Web development]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[webdesign]]></category>

		<guid isPermaLink="false">http://www.jensbits.com/?p=336</guid>
		<description><![CDATA[jQuery files updated June 23, 2010 If you liked this post, you might also like the jQuery.ajax and jQuery.post Form Submit Examples with PHP post. I wanted to make a nice modal dialog box that would confirm submission of a form. And, specifically, it had to ask if their e-mail address was correct that they [...]


Related posts:<ol><li><a href='http://www.jensbits.com/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion'>jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion</a></li>
<li><a href='http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with PHP'>jQuery.ajax and jQuery.post Form Submit Examples with PHP</a></li>
<li><a href='http://www.jensbits.com/2010/06/16/jquery-modal-dialog-close-on-overlay-click/' rel='bookmark' title='Permanent Link: jQuery Modal Dialog Close on Overlay Click'>jQuery Modal Dialog Close on Overlay Click</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>jQuery files updated June 23, 2010</strong></p>
<blockquote><p>If you liked this post, you might also like the <a href="/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/">jQuery.ajax and jQuery.post Form Submit Examples with PHP</a> post.</p></blockquote>
<p>I wanted to make a nice modal dialog box that would confirm submission of a form. And, specifically, it had to ask if their e-mail address was correct that they listed on the form. Typos, particularly transposed letters, cause a number of undeliverable e-mails. Making matters worse, sometimes by the time they get to the &#8216;Submit&#8217; button, the all important e-mail field is no longer in view. Yes, despite the web developer&#8217;s best efforts, people still manage to type in their own e-mail incorrectly. I&#8217;ve done it. We&#8217;re all human.</p>
<p>I already knew about the javascript 1.0 method of &#8216;return confirm&#8217; but I wanted more control over the look and feel. As an added requirement, I wanted to use Thickbox since I already had that loaded into the app. I don&#8217;t like throwing in a bunch of js files for every little nuance. If I can use what I already have, it makes the app much leaner.</p>
<p>So, I Googled it. I only found partial solutions. I wanted to go all the way and have the modal box pop up whether the user hit &#8216;Submit&#8217; or whether they hit enter on the keyboard in as many browsers as possible (hint: IE). Maybe I didn&#8217;t look hard enough, but I ended up creating the solution on my own.</p>
<p>Here are three examples (including <a href="http://jensbits.com/demos/forms/index.php">a complete demo</a>) of a form submission confirmation dialog. One each for just javascript, the jQuery UI dialog box, and Thickbox.</p>
<h2>Javascript 1.0</h2>
<p>For pure javascript it&#8217;s painfully straightforward:</p>
<pre class="brush: xml;">
&lt;form id=&quot;testconfirmJS&quot; name=&quot;testconfirmJS&quot; method=&quot;post&quot; onsubmit=&quot;return confirm('You entered your e-mail address as:\n\n' + document.getElementById('emailJS').value + '\n\nSelect OK if correct or Cancel to edit.')&quot;&gt;&gt;
&lt;fieldset&gt;
&lt;legend&gt;Javacript Return Confirm&lt;/legend&gt;
&lt;p&gt;&lt;label for=&quot;email&quot;&gt;E-mail:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;emailJS&quot; type=&quot;text&quot; name=&quot;emailJS&quot; value=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
</pre>
<p>And you get something that looks like this:<br />
<img src="http://jensbits.com/images/js-JS-FormSubmit.gif" alt="javascript confirm box" /></p>
<p>Low overhead, not too much strain on the brain but pretty plain vanilla. And boring. Let&#8217;s do something a little fancier.</p>
<h2>jQuery UI</h2>
<p>The <a href="http://jqueryui.com/">jQuery UI library</a> is awesome. Loads of flexibility and customization available and they hand you everything you need. For this solution we have to load in an additional stylesheet (you can <a href="http://jqueryui.com/themeroller/">download a custom ready-made css</a>), the jQuery base js, and the jQuery UI js. The jQuery UI site allows you to customize its js file to include only the parts you will use.</p>
<pre class="brush: xml;">
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/blitzer/jquery-ui-1.8.2.custom.css&quot;&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.4.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-ui-1.8.2.custom.min.js&quot;&gt;&lt;/script&gt;
</pre>
<p>The form is untouched for this approach, but we do have to add some js to the document itself and an additional div element that will hold the contents of our modal box.</p>
<p>Here the we set some parameters for the dialog box including what the buttons should say and do. Then a little jQuery is added so that the e-mail is displayed in the box and the box opens on submit allowing the user to hit the &#8216;Submit&#8217; button or the enter key.</p>
<pre class="brush: jscript;">
        $(function(){
                // jQuery UI Dialog    

                $('#dialog').dialog({
                    autoOpen: false,
                    width: 400,
                    modal: true,
                    resizable: false,
                    buttons: {
                        &quot;Submit Form&quot;: function() {
                            document.testconfirmJQ.submit();
                        },
                        &quot;Cancel&quot;: function() {
                            $(this).dialog(&quot;close&quot;);
                        }
                    }
                });

                $('form#testconfirmJQ').submit(function(){
                    $(&quot;p#dialog-email&quot;).html($(&quot;input#emailJQ&quot;).val());
                    $('#dialog').dialog('open');
                    return false;
                });
        });
</pre>
<p>The form is pretty clean and the div containing the text for the dialog box can be placed anywhere on the page.</p>
<pre class="brush: xml;">

&lt;form id=&quot;testconfirmJQ&quot; name=&quot;testconfirmJQ&quot; method=&quot;post&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;jQuery UI Modal Submit&lt;/legend&gt;
&lt;p&gt;&lt;label for=&quot;email&quot;&gt;E-mail:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;emailJQ&quot; type=&quot;text&quot; name=&quot;emailJQ&quot; value=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;

&lt;div id=&quot;dialog&quot; title=&quot;Verify Form jQuery UI Style&quot;&gt;&lt;p&gt;&lt;span class=&quot;ui-icon ui-icon-alert&quot; style=&quot;float:left; margin:0 7px 0 0;&quot;&gt;&lt;/span&gt; You entered your e-mail address as:&lt;/p&gt;&lt;p id=&quot;dialog-email&quot;&gt;&lt;/p&gt;&lt;p&gt;
If this is correct, click Submit Form.&lt;/p&gt;&lt;p&gt;To edit, click Cancel.&lt;p&gt;&lt;/div&gt;
</pre>
<p>Using this method you get a much prettier result:<br />
<img src="http://jensbits.com/images/js-JQ-UI-FormSubmit.gif" alt="jQuery dialog box" /></p>
<h2>Thickbox method</h2>
<p>The <a href="http://jquery.com/demo/thickbox/">ever helpful and massively adaptable Thickbox</a> was easily coded to do this as well. Thickbox also requires its own stylesheet and js along with the base jQuery file. Many apps have this already loaded since Thickbox is so wildly popular.</p>
<pre class="brush: xml;">
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/thickbox.css&quot;&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/thickbox-compressed.js&quot;&gt;&lt;/script&gt;
</pre>
<p>As with the jQuery UI method, we add some js to the document to put in the e-mail from the form, open the Thickbox, and instruct the buttons on what actions to take when clicked.</p>
<pre class="brush: jscript;">
        $(function(){
                //Thickbox

                $('form#testconfirmTB').submit(function(){
                    $(&quot;p#TB-email&quot;).html($(&quot;input#emailTB&quot;).val());
                    tb_show('Verify Form Thickbox Style','TB_inline?height=155&amp;amp;width=300&amp;amp;inlineId=TBcontent');
                    return false;
                });

                $('input#TBcancel').click(function(){
                    tb_remove();
                });

                $('input#TBsubmit').click(function(){
                    document.testconfirmTB.submit();
                });
        });
</pre>
<p>The form is also clean for this method and a div is added for the Thickbox content.</p>
<pre class="brush: xml;">
&lt;form id=&quot;testconfirmTB&quot; name=&quot;testconfirmTB&quot; method=&quot;post&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;Thickbox Modal Submit&lt;/legend&gt;
&lt;p&gt;&lt;label for=&quot;email&quot;&gt;E-mail:&lt;/label&gt;&lt;br /&gt;
&lt;input id=&quot;emailTB&quot; type=&quot;text&quot; name=&quot;emailTB&quot; value=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;

&lt;/fieldset&gt;
&lt;/form&gt;

&lt;div id=&quot;TBcontent&quot; style=&quot;display: none;&quot;&gt;&lt;p&gt;You entered your e-mail address as:&lt;/p&gt;&lt;p id=&quot;TB-email&quot;&gt;&lt;/p&gt;&lt;p&gt;
If this is correct, click Submit Form.&lt;/p&gt;&lt;p&gt;To edit, click Cancel.&lt;p&gt;
&lt;input type=&quot;submit&quot; id=&quot;TBcancel&quot; value=&quot;Cancel&quot; /&gt;
&lt;input type=&quot;submit&quot; id=&quot;TBsubmit&quot; value=&quot;Submit Form&quot; /&gt;
&lt;/div&gt;
</pre>
<p>The Thickbox, generically styled (the default styling actually), looks like this:<br />
<img src="http://jensbits.com/images/js-TB-FormSubmit.gif" alt="Thickbox modal confirm box" /></p>
<p>Three flavors for confirming on form submission. Enjoy.</p>
<h2>Recommended:</h2>
<div style="height:250px;">
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0596159773" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=1935182323" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0980576857" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="float:left;margin-right: 25px">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=jensbits-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0321509021" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
</div>
<p id="demo"><a href="/demos/forms/index.php"><span>Demo</span></a></p>
<p id="download"><a href="/media/code/modal-confirm.zip"><span>Download zip of all files</span></a></p>
<p class="donate">If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.</p>

<!-- Begin PayPal Donations by http://wpstorm.net/ -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="jen@jensbits.com" /><input type="hidden" name="return" value="http://www.jensbits.com/thank-you/" /><input type="hidden" name="item_name" value="Help pay hosting. All donations go to hosting fees for this site." /><input type="hidden" name="currency_code" value="USD" /><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
<!-- End PayPal Donations -->



<p>Related posts:<ol><li><a href='http://www.jensbits.com/2009/10/23/jquery-ajax-and-jquery-post-form-submit-examples-with-coldfusion/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion'>jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion</a></li>
<li><a href='http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/' rel='bookmark' title='Permanent Link: jQuery.ajax and jQuery.post Form Submit Examples with PHP'>jQuery.ajax and jQuery.post Form Submit Examples with PHP</a></li>
<li><a href='http://www.jensbits.com/2010/06/16/jquery-modal-dialog-close-on-overlay-click/' rel='bookmark' title='Permanent Link: jQuery Modal Dialog Close on Overlay Click'>jQuery Modal Dialog Close on Overlay Click</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/feed/</wfw:commentRss>
		<slash:comments>75</slash:comments>
		</item>
		<item>
		<title>Internet Explorer (IE) Not Submitting Form on Enter</title>
		<link>http://www.jensbits.com/2009/08/03/internet-explorer-ie-not-submitting-form-on-enter/</link>
		<comments>http://www.jensbits.com/2009/08/03/internet-explorer-ie-not-submitting-form-on-enter/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 13:52:31 +0000</pubDate>
		<dc:creator>jen</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[IE]]></category>

		<guid isPermaLink="false">http://www.jensbits.com/?p=332</guid>
		<description><![CDATA[While working on a ColdFusion application, I noticed that the login form was not submitted when enter was pressed in Internet Explorer. Other browsers, Firefox and Chrome specifically, did not have this issue. There are many methods used to fix this error if you Google it, but I found that the simplest solutions are usually [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>While working on a ColdFusion application, I noticed that the login form was not submitted when enter was pressed in Internet Explorer. Other browsers, Firefox and Chrome specifically, did not have this issue. There are many methods used to fix this error if you Google it, but I found that the simplest solutions are usually the best. No javascript, no funky CSS to hide fields, yada, yada, yada.</p>
<h3>The Situation</h3>
<p>This app had a form with a single text field and a submit button. The form submitted back to its own page were the logic executed if form.submit (the submit button itself) came back with the form variables collection.</p>
<p>When you pressed the enter button, IE did not send the input type=submit field back with the form vars and the form logic did not execute. The page appeared to refresh and you started over again.</p>
<h3>The Solution</h3>
<p>All that was necessary was to add a hidden input field that the form logic used instead.</p>
<pre class="brush: xml;">
&lt;input type=&quot;hidden&quot; name=&quot;login&quot; value=&quot;loginSubmit&quot; /&gt;
</pre>
<p>If form.login and form.login = &#8216;loginSubmit&#8217;, the form logic executes and the user is logged in.</p>
<p>So, from now on, I am not going to rely on the input submit button to check for form submission. Call me crazy, but I think IE has the right idea here; the input submit should not be part of the form variables collection.</p>
<p class="donate">If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.</p>

<!-- Begin PayPal Donations by http://wpstorm.net/ -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="jen@jensbits.com" /><input type="hidden" name="return" value="http://www.jensbits.com/thank-you/" /><input type="hidden" name="item_name" value="Help pay hosting. All donations go to hosting fees for this site." /><input type="hidden" name="currency_code" value="USD" /><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
<!-- End PayPal Donations -->



<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jensbits.com/2009/08/03/internet-explorer-ie-not-submitting-form-on-enter/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
