Skip to content

Instantly share code, notes, and snippets.

@txus
Created June 5, 2013 09:50
Show Gist options
  • Save txus/5712825 to your computer and use it in GitHub Desktop.
Save txus/5712825 to your computer and use it in GitHub Desktop.
Disabled and checked checkboxes aren't sent in post requests.
# app.rb
#
# Usage:
#
# $ gem install sinatra
# $ ruby app.rb
#
require 'sinatra'
get '/' do
erb :index
end
post '/foo' do
puts params # {"checked"=>"on", "submit"=>"Send"}
end
__END__
@@ layout
<html>
<%= yield %>
</html>
@@ index
<h1>Hello</h1>
<form action="/foo" method="post">
<input type="checkbox" name="checked" checked="true">Checked</input>
<input type="checkbox" name="unchecked">Unchecked</input>
<input type="checkbox" name="disabled" disabled="true">Disabled</input>
<input type="checkbox" name="disabled_and_checked" checked="true" disabled="true">Disabled and checked</input>
<input type="submit" name="submit" value="Send"/>
</form>
@andrewslotin
Copy link

Which allows us to do this trick:

<input type="hidden" name="check_me" value="0"/>
<input type="checkbox" name="check_me" value="1"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment