<?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>Blog of a LAMP Developer based in Richmond, Surrey &#187; Javascript</title>
	<atom:link href="http://www.lampdeveloper.co.uk/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lampdeveloper.co.uk</link>
	<description>A day in the life of a Lamp Developer</description>
	<lastBuildDate>Sat, 13 Nov 2010 11:16:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Jquery to submit a form and run with Cakephp Baked Pages</title>
		<link>http://www.lampdeveloper.co.uk/cakephp/using-jquery-to-submit-a-form-and-run-with-cakephp-baked-pages.html</link>
		<comments>http://www.lampdeveloper.co.uk/cakephp/using-jquery-to-submit-a-form-and-run-with-cakephp-baked-pages.html#comments</comments>
		<pubDate>Wed, 11 Mar 2009 15:34:14 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Cakephp]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Jquery]]></category>

		<guid isPermaLink="false">http://www.lampdeveloper.co.uk/?p=86</guid>
		<description><![CDATA[I found a simple solution for submitting a form using jquery + ajax. One that was compatible with CakePHP.
You need to create a from with an ID.


&#60;?php echo $form-&#62;create(&#039;Test&#039;,array(&#039;url&#039; =&#62; &#039;edit/&#039;, &#039;type&#039; =&#62; &#039;get&#039;,&#039;id&#039; =&#62; &#039;TestID&#039;));?&#62;

Then bind a listener to the form submit


// do an ajax from instead
$(&#039;#TestID&#039;).submit(function() {

var inputs = [];
$(&#039;:input&#039;, this).each(function() {
inputs.push(this.name + [...]]]></description>
			<content:encoded><![CDATA[<p>I found a simple solution for submitting a form using jquery + ajax. One that was compatible with CakePHP.</p>
<p>You need to create a from with an ID.</p>
<pre name="code" class="php">

&lt;?php echo $form-&gt;create(&#039;Test&#039;,array(&#039;url&#039; =&gt; &#039;edit/&#039;, &#039;type&#039; =&gt; &#039;get&#039;,&#039;id&#039; =&gt; &#039;TestID&#039;));?&gt;
</pre>
<p>Then bind a listener to the form submit</p>
<pre name="code" class="javascript">

// do an ajax from instead
$(&#039;#TestID&#039;).submit(function() {

var inputs = [];
$(&#039;:input&#039;, this).each(function() {
inputs.push(this.name + &#039;=&#039; + escape(this.value));
})

jQuery.ajax({
data: inputs.join(&#039;&amp;amp;amp;amp;amp;&#039;),
url: this.action,
timeout: 2000,
error: function() {
console.log(&quot;Failed to submit&quot;);
},
success: function(r) {
console.log(r);
}
})

// return false
return false;
});
</pre>
<p>This will work with checkboxes, input fields, selects but not upload file types.</p>
<p>It will create a get string and send the request to the form action using ajax.</p>
<p>CakePHP usually uses $this-&gt;data in its baked pages. As it is now a get request, I will now be using $this-&gt;params.</p>
<p>If you want to use Cake&#8217;s baked pages, just add this ternary operator to the beginning of the corresponding methods.</p>
<pre name="code" class="php">

$this-&gt;data = isset($this-&gt;params[&#039;url&#039;][&#039;data&#039;])?$this-&gt;params[&#039;url&#039;][&#039;data&#039;]:$this-&gt;data;
</pre>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Blog%20of%20a%20LAMP%20Developer%20based%20in%20Richmond%2C%20Surrey&amp;siteurl=http%3A%2F%2Fwww.lampdeveloper.co.uk%2F&amp;linkname=Using%20Jquery%20to%20submit%20a%20form%20and%20run%20with%20Cakephp%20Baked%20Pages&amp;linkurl=http%3A%2F%2Fwww.lampdeveloper.co.uk%2Fcakephp%2Fusing-jquery-to-submit-a-form-and-run-with-cakephp-baked-pages.html"><img src="http://www.lampdeveloper.co.uk/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://www.lampdeveloper.co.uk/cakephp/using-jquery-to-submit-a-form-and-run-with-cakephp-baked-pages.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

