<?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>No Rest For The Weekend &#187; html</title>
	<atom:link href="http://www.norestfortheweekend.com/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.norestfortheweekend.com</link>
	<description>Time is an illusion. Weekends doubly so.</description>
	<lastBuildDate>Mon, 22 Mar 2010 09:48:46 +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>On forms, submit buttons and browsers</title>
		<link>http://www.norestfortheweekend.com/2009/10/20/on-forms-submit-buttons-and-browsers/</link>
		<comments>http://www.norestfortheweekend.com/2009/10/20/on-forms-submit-buttons-and-browsers/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 09:10:52 +0000</pubDate>
		<dc:creator>markstickley</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[submit]]></category>

		<guid isPermaLink="false">http://www.norestfortheweekend.com/?p=59</guid>
		<description><![CDATA[Forms are tricky beasts and no less so when they behave differently in different browsers. This post explores how different browsers react to submit buttons in different ways, and how to get around it.]]></description>
			<content:encoded><![CDATA[<p>Aah, ambiguity! What a tricky devil you are. The <a href="http://www.w3.org">W3C</a> Recommendations are normally very specific and not at all ambiguous, but when things are left open to interpretation you can be fairly sure of varying results.</p>
<h4>Bad form, old chap</h4>
<p>Have a look at this form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;wherever.html&quot; method=&quot;post&quot;&gt;
    &lt;label for=&quot;firstname&quot;&gt;First name&lt;/label&gt;
    &lt;input name=&quot;firstname&quot; id=&quot;firstname&quot; type=&quot;text&quot; /&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;proceed&quot; value=&quot;Proceed&quot; /&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;back&quot; value=&quot;Back&quot; /&gt;
&lt;/form&gt;</pre></div></div>

<p>What gets submitted when you hit Proceed? Well yes, <code>firstname</code> is included as is <code>proceed</code>, but is <code>back</code>? What gets submitted when you hit Back, as the second submit button in the form?</p>
<p>Well the <a href="http://www.w3.org/TR/html401/interact/forms.html#submit-format">W3C Guidelines on form submission</a> say that for forms with more than one submit button, only the submit button that was pressed should be submitted. This seems fair enough.</p>
<p><strong>But what gets submitted when you press enter from within the text field?</strong></p>
<p>The W3C has no recommendation for this! The precise wording used to qualify whether a submit button gets sent along with the other form data is:</p>
<blockquote cite="http://www.w3.org/TR/html401/interact/forms.html#submit-format"><p>If a form contains more than one submit button, only the activated submit button is successful.</p></blockquote>
<p>&#8230;where &#8216;activated&#8217; means having been clicked on to submit the form and being &#8217;successful&#8217; refers to the selection process for including form elements in the request.</p>
<h4>Pressing the right buttons (or not, as the case may be)</h4>
<p>What actually happens varies between browsers, which I suppose is no surprise really. The surprise is which browsers do what.</p>
<p>We see two behaviors here:</p>
<ul>
<li>IE (I tested 6, 7 &#038; 8) works on the basis that you only submit an <em>activated</em> form element and the only way to activate a submit button is to <em>click on it</em>. Therefore if you click on a submit button, IE will submit it&#8217;s value along with the rest of the form, but if you press <em>Enter</em> to submit it doesn&#8217;t submit the value of <em>any</em> submit buttons.</li>
<li>All other browsers (tested: Firefox, Safari, Chrome, Opera) submit a value for a submit button, <em>whether or not you click it</em>. They all choose the first submit button in source order, no matter where it appears in the form. Thank goodness that&#8217;s consistent!</li>
</ul>
<p>I guess that the other browsers think it&#8217;s fair to assume that pressing enter while entering data into a form is the same as clicking on the submit button, which in most cases it is. But what happens when you&#8217;ve got two or more submit buttons? How do you know which button the user wanted to click? How can the developer predict that, supposing they even know about this peculiarity? Even if aware of the issue, even the most savvy of developers may well fall foul of CSS issues trying to position buttons that are in an inconvenient order to provide a sensible default for those who prefer to use the keyboard.</p>
<p>Yes, this time <em>I think IE got it right</em>!</p>
<h4>How to fix the problem</h4>
<p>I think anyone that works with browsers will admit that we all dream of a world where they all have consistent and good behavior. But failing that, consistent behavior would be nice. </p>
<p>I&#8217;m not convinced that all the other browsers will change their behavior any time soon (even if they could be convinced that what they are doing isn&#8217;t the right option) as there are probably hundreds of thousands of sites out there that will break if it changes. So the best thing to do is to &#8216;fix&#8217; IE to behave the same.</p>
<p>Here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;wherever.html&quot; method=&quot;post&quot;&gt;
    &lt;label for=&quot;firstname&quot;&gt;First name&lt;/label&gt;
    &lt;input name=&quot;firstname&quot; id=&quot;firstname&quot; type=&quot;text&quot; /&gt;
&nbsp;
    &lt;input type=&quot;hidden&quot; name=&quot;submit_button&quot; value=&quot;Proceed&quot; /&gt;
    &lt;input type=&quot;submit&quot; name=&quot;submit_button&quot; value=&quot;Proceed&quot; /&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;submit_button&quot; value=&quot;Back&quot; /&gt;
&lt;/form&gt;</pre></div></div>

<p>You&#8217;ll notice I changed a few things around! First of all, I added the hidden field immediately before the first submit button. The first button is the one which acts as the default button in FF, Safari <em>et al.</em> and so we are making it do the same in IE. By adding a hidden field with the same name and value as the first button <strong>immediately before</strong> the button itself, it effectively acts as a default. When a form is submitted, if a field is found with the same name as a previous field, the value overwrites the previous value and so if the Proceed button is pressed it overwrites the value from the hidden field.</p>
<p>In IE if the user presses Enter to submit the form, neither button is pressed and the hidden field gets submitted. But as it has the same name and value as the Proceed button, it&#8217;s as if the Proceed button was pressed. In the other browsers the value of the Proceed button overwrites the value of the hidden field and so it&#8217;s like it&#8217;s not even there.</p>
<p>That&#8217;s all fine but if the Back button is clicked the hidden field will be submitted as well giving an impossible value for both the submit buttons in the same request! That&#8217;s why I&#8217;ve also changed the name of the Back button to be the same as the name of the Proceed button &#8211; that way there will always be a value submitted for <code>submit_button</code>: either &#8220;Back&#8221; or the default &#8220;Proceed&#8221;.</p>
<p><em>Notice I didn&#8217;t call any of the fields &#8220;<code>submit</code>&#8220;. That was intentional, and it&#8217;s because if we ever want to submit the form via Javascript having a field named <code>submit</code> would make that impossible.</em></p>
<h4>But is it really a problem?</h4>
<p>That solution is a bit clunky to be honest. Having the extra field is a bit of a hack and the fact that you have to call the hidden field and both submit buttons by the same name make it a little restrictive, especially when it doesn&#8217;t even need to be a problem.</p>
<p>Armed with the knowledge in this article we know that we can&#8217;t rely on the values of the submit buttons to be broadcast. If we don&#8217;t know which button has been clicked, we simply choose a default action and perform that unless we detect the alternative action. In PHP and for the first form it would go something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// if a form was submitted</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'back'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// if the back button was clicked</span>
		<span style="color: #666666; font-style: italic;">// Perform back action </span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Perform default action</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So long as you don&#8217;t assume the button to be clicked in order to perform the default action, life will be good!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.norestfortheweekend.com/2009/10/20/on-forms-submit-buttons-and-browsers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REST For The Weekend</title>
		<link>http://www.norestfortheweekend.com/2009/08/25/rest-for-the-weekend/</link>
		<comments>http://www.norestfortheweekend.com/2009/08/25/rest-for-the-weekend/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 15:05:20 +0000</pubDate>
		<dc:creator>markstickley</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[architectures]]></category>
		<category><![CDATA[crud]]></category>
		<category><![CDATA[mime]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[restful]]></category>

		<guid isPermaLink="false">http://www.norestfortheweekend.com/?p=24</guid>
		<description><![CDATA[What is REST? People talk about it like it's a good thing. It is, but not in all cases. This post teaches you about the basics of REST so you can make the right decision.]]></description>
			<content:encoded><![CDATA[<p>Oh well, the weekend&#8217;s over again. What did you do? I got an impressive sunburn, bought some delicious fruit and veg at the market and looked into RESTful architectures, among other things. Although I would love to tell you about my culinary adventures, what I&#8217;m going to write about now is REST. It should be noted that in this case REST is not sitting in front of the TV and has nothing to do with the title of this blog, despite what you may think!</p>
<h4>What REST is not</h4>
<p>There seems to be some confusion over exactly what a RESTful architecture is. Some people think it is pretty URLs:</p>
<pre>

http://www.mysite.com/portfolio/design/someclient
</pre>
<p>as opposed to:</p>
<pre>
http://www.mysite.com/index.php?section=portfolio »
    &#038;subsection=design&#038;client=someclient
</pre>
<p>This is not RESTful.</p>
<p>Some people think REST is a bunch of actions triggered by visiting URLs:</p>
<pre>

http://www.mysite.com/contact/email/send
</pre>
<p>This isn&#8217;t RESTful either.</p>
<h4>Being resourceful</h4>
<p>REST stands for <strong>RE</strong>presentational <strong>S</strong>tate <strong>T</strong>ransfer and it&#8217;s all about resources. That is to say, data presented in a formatted way and made available at specific, addressable points of entry. Most of the time when people talk about a REST interface they will be talking about the web and URLs, but REST is not limited to this technology. So long as each resource on a system can be identified uniquely, manipulated, each request for resources includes enough information that the client knows how to process it and has the ability to contain references to other resources then it can be called RESTful.</p>
<p>A RESTful system is a stateless system; each resource request in a RESTful system should be completely independent of any other. It should not rely on cookies, session variables or any other state maintaining solutions. This is because a REST request is intended to be performed on the current state of the specified resource and the resource should be able to be encapsulated in a single response.</p>
<h4>Strict instructions</h4>
<p>It might have slipped under the radar but a couple of paragraphs above I mentioned that a REST response should include information for the client on how to process the data. In the case of a web service (and it should be assumed from here on out I&#8217;ll be talking about web services) the <a href="http://en.wikipedia.org/wiki/MIME_type">MIME type</a> should be sufficient. For example, if you are serving your response up as JSON you&#8217;d want to use the <strong>MIME type</strong>: <code>application/json</code>. The <strong>MIME type</strong> is contained within the header of the response.</p>
<p>In a similar manner, the client needs to tell the server what it wants to do with the specific resource. We can do this by specifying the correct <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods">HTTP Method</a> as part of the request.</p>
<h4>Method in madness</h4>
<p>So the HTTP methods most people are used to are: <code>GET</code> (the default method, what you get when you hit enter after typing a URL) and <code>POST</code> (the method used for submitting most forms). The others we are interested in are <code>PUT</code> and <code>DELETE</code>. Using these four methods we can instruct a RESTful interface to perform the basic <abbr title="Create, Read, Update, Delete">CRUD</abbr> operations:</p>
<ul>
<li><strong>C</strong>reate = <code>POST</code></li>
<li><strong>R</strong>ead = <code>GET</code></li>
<li><strong>U</strong>pdate = <code>PUT</code></li>
<li><strong>D</strong>elete = <code>DELETE</code> (rather unsurprisingly)</li>
</ul>
<p>Using these methods you can manipulate a resource in any way you like from that single addressable entry point. If you have the URL for the resource, in a RESTful architecture you know you won&#8217;t have to append strings to the URL to make it do things (Eg <code>?action=delete</code>) &#8211; you just change the method used to request the resource in the request header.</p>
<p>Obviously if your system is externally accessible you will probably want to limit access to the &#8216;unsafe&#8217; <code>POST</code>, <code>PUT</code> and <code>DELETE</code> methods.</p>
<h4>Not for everyone</h4>
<p>So as you can tell, REST isn&#8217;t something you should strive to include just for the sake of it. I can&#8217;t stress this enough: <em>there&#8217;s no point in shoehorning REST into a site or project when it is not really needed</em>. The main use for it it most likely to be for Web Service APIs and anything that is highly resource driven. Think of it as a series of defined views on a database or other resource and you can&#8217;t go too far wrong.</p>
<p>I am currently working on a web based file system for storing assets as part of a CMS. It has an API so that you can add, edit, read and delete assets from external locations, providing that you present the right credentials. This almost seems like the perfect application for REST as each entry &#8211; be it a file or a folder &#8211; needs to have the basic CRUD operations performed on it. The whole URL structure is basically the directory tree and every point in that tree is addressable and will accept all the different methods.</p>
<h4>Testing, testing</h4>
<p>This is all very well, but how do you go about testing these crazy HTTP methods, practically? You can&#8217;t have a page full of links to click because a link is a <code>GET</code> request. There&#8217;s no way of changing that. </p>
<p>Forms offer a little flexibility &#8211; you can specify the method you want to use with the method attribute. Unfortunately this is restricted to <code>GET</code> and <code>POST</code>. If you try anything other than that it will default to <code>GET</code>.</p>
<p>To test these request methods, you have to hand craft an HTTP request. You can do this in the server-side scripting language of your choice (Eg PHP, Ruby, Python etc) <em>OR</em> you can download one of the handy Firefox extensions that kindly developers have made available for nothing for this very purpose! My personal favourite is <a href="https://addons.mozilla.org/en-US/firefox/addon/9780">RESTClient</a> by <a href="http://www.porphyry.org/">Chao ZHOU</a> but there are plenty of others for you to choose from. Here are a few:</p>
<ul>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/2691">Poster</a> by <a href="http://www.milowski.com/">Alex Milowski</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/5946">RESTTest</a> by <a href="http://www.xucia.com/">kriszyp</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/967">Modify Headers</a> by <a href="http://www.garethhunt.com/">Gareth Hunt</a></li>
</ul>
<h4>Using RESTClient</h4>
<p>It&#8217;s pretty simple really. The basic interface looks like this:</p>
<div id="attachment_25" class="wp-caption aligncenter" style="width: 628px"><img src="http://www.norestfortheweekend.com/wp-content/uploads/2009/08/restclient.gif" alt="The RESTClient interface displays fields for method, url, header and request body along with an area in which to display the response. At the top are controls to open &amp; save your request amongst other things." title="RESTClient screenshot" width="618" height="603" class="size-full wp-image-25" /><p class="wp-caption-text">The RESTClient interface displays fields for method, url, header and request body along with an area in which to display the response. At the top are controls to open &#038; save your request amongst other things.</p></div>
<ol>
<li>Choose your request method</li>
<li>Type the resource location</li>
<li>Add any request headers you need (this is like items from form fields)</li>
<li>Type the request body you want to transmit (this can be left blank most of the time)</li>
<li>Hit send</li>
</ol>
<p>What you will get back is the response as the server sends it. You can inspect the header and the body to make sure it&#8217;s coming back as you&#8217;d expect and so your application that will consume the response won&#8217;t get any nasty surprises!</p>
<p>Hopefully you are interested in RESTful architectures now, and are feeling inspired to go off and write one of your own!</p>
<p class="furtherreading">For more information on this topic, check out the <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">Wikipedia article on REST</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.norestfortheweekend.com/2009/08/25/rest-for-the-weekend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->