{"id":228,"date":"2017-08-11T15:27:05","date_gmt":"2017-08-11T20:27:05","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=228"},"modified":"2019-06-15T12:41:02","modified_gmt":"2019-06-15T17:41:02","slug":"python-on-the-web-how-to-get-cgi-script-to-recognize-two-submit-buttons-with-separate-values-in-one-form","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/11\/python-on-the-web-how-to-get-cgi-script-to-recognize-two-submit-buttons-with-separate-values-in-one-form\/","title":{"rendered":"Python on the web: How to get a CGI script to recognize two submit buttons with separate values in one form"},"content":{"rendered":"<p>In one of my Python cgi scripts I have a web form with some hidden values, text fields, and two submit buttons. I want my cgi script to do something different depending on which submit button was clicked.<\/p>\n<p>One of the submit buttons is for completing a review without making any changes and is called &#8220;Submit No Changes&#8221;. The other submit button is for the case where the reviewer made changes in the form, and is called &#8220;Capture Edits&#8221;. Both of these submit buttons are in the same HTML web form and point to the same cgi script.<\/p>\n<p>At first I set up my submit buttons like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;input type=\"submit\" name=\"capture_edits\" value=\"Capture Edits\"&gt;\n&lt;input type=\"submit\" name=\"submit_no_changes\" value=\"Submit No Changes\"&gt;<\/pre>\n<p>but I noticed that neither the names or values from these buttons were being captured by <code>cgi.FieldStorage()<\/code>\u00a0. After sending a POST to the cgi script by clicking the &#8220;Capture Edits&#8221; submit button, the name &#8220;capture_edits&#8221; wasn&#8217;t being captured in the list of form keys when I printed them like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import cgi\nform = cgi.FieldStorage()\n\nprint \"Content-type: text\/html\"\nprint \"\"\nprint form.keys()<\/pre>\n<p>It turns out, the name-value pairs from the submit button that was clicked will only be captured by <code>cgi.FieldStorage()<\/code> when <code>type=\"submit\"<\/code> is listed to the right of the name and value. For example, this worked:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;input name=\"capture_edits\" value=\"Capture Edits\" type=\"submit\"&gt;\n&lt;input name=\"submit_no_changes\" value=\"Submit No Changes\" type=\"submit\"&gt;<\/pre>\n<p>Perhaps this is because the browser doesn&#8217;t pay attention to anything after it sees the key word &#8220;submit&#8221; when a form POST is submitted?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my Python cgi scripts I have a web form with some hidden values, text fields, and two submit buttons. I want my cgi script to do something different depending on which submit button was clicked. One of the submit buttons is for completing a review without making any changes and is called &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/11\/python-on-the-web-how-to-get-cgi-script-to-recognize-two-submit-buttons-with-separate-values-in-one-form\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python on the web: How to get a CGI script to recognize two submit buttons with separate values in one form<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,22],"tags":[16,20,4],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-python-cgi","category-python-language","tag-cgi","tag-html-forms","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":13,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":2782,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/228\/revisions\/2782"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}