Tuesday, March 22, 2016

Submit Buttons: More Useful than You Think

We all are familiar with using a submit button to post a html form back to the server; however, people often overlook their name attribution which can provide the server with additional information to give better context to the request and the user's intent.

I like to name all my submit buttons Action as a matter of protocol. But obviously, you can pick your poison. When you name your different submit buttons all the same thing, they sort of act like a select box with each button representing a submission option.


Client Side:
    <label for="MyFavQuote" >Fav Quote: </label> 
    <input id="MyFavQuote" name="MyFavQuote" type="text" />

    <input id="SaveDraftButton" name="Action" type="submit" value="Save as Draft" />

    <input id="SaveFinalButton" type="submit" name="Action" value="Save as Final" />


Server Side:
String quote = Request["MyFavQuote"];
String action = Request["Action"];
if (action == null)
{

      //Nothing needed for get request
}
else if (action == "Save as Draft")
{
     data.SaveDraft(userId, quote);
}
else if (action == "Save as Final")
{
     data.Publish(userId, quote);
}