0

I am working on a form that have two submit buttons.

According to this SO accepted answer, one could know which submit button was pressed in PHP doing something like:

<input type="submit" name="publish" value="Publish">
<input type="submit" name="save" value="Save">

<?php
    if (isset($_POST['publish'])) {
        # Publish-button was clicked
    }
    elseif (isset($_POST['save'])) {
        # Save-button was clicked
    }
?>

Now, I need to do the same with Python, if it is possible. At present, I am retrieving the POSTed values like:

POST={}
args=sys.stdin.read().split('&')

for arg in args:
    t=arg.split('=')
    if len(t)>1: k, v=arg.split('='); POST[k]=v

However, doing so I am only able to know the values of my form, not if they were actually set.

Something like if POST.get('publish', 'default_value') will always evaluate to True as in my form, its value (Publish) is set.

What can I do to mimic the PHP isset? is there a way to do it in Python preferably without fameworks?

EDIT

After @04FS comment, I ran a simple test to check whether my assumption was right and indeed it seems to be.

This is my test. I press one of the two button, but, regardless, I receive:

import sys
import cgi
import cgitb

cgitb.enable()
print ("Content-type: text/html\n\n")

# https://stackoverflow.com/a/27893309/1979665
POST={}
args=sys.stdin.read().split('&')

for arg in args:
    t=arg.split('=')
    if len(t)>1: k, v=arg.split('='); POST[k]=v

if POST.get('publish', 'default_value'):
    print("<p>Publish was pressed</p>")

if POST.get('save', 'default_value'):
    print("<p>Save was pressed</p>")

The output is:

Publish was pressed

Save was pressed
umbe1987
  • 2,894
  • 6
  • 35
  • 63
  • 1
    _“only able to know the values of my form, not if they were actually set”_ - no clue what you mean by that. If the `publish` button was not clicked, then it does not become part of the form submission data set in the first place. – 04FS Mar 14 '19 at 10:15
  • @04FS thank for your observation, I'll test the code again and check if the code actually evaluates to True although I didn't press the publish sumbmit button. it's a bit hard to debug but I'll figure it out and update with my findings. Thanks. – umbe1987 Mar 14 '19 at 10:20
  • @04FS Just tested, apparently, it seems I cannot say which button was pressed, I'll update my question with the result. – umbe1987 Mar 14 '19 at 10:32
  • 1
    I think your mistake is that you are supplying a default value here. If `publish` was not set in the input data, then you _make it_ have the value `default_value`. That is a true-y value when you check it via if, so you get both outputs. You only want `POST.get('publish')` - that either returns a value, if the parameter actually exists - or null/false/whatever (don’t know the details of this in python), if it didn’t. – 04FS Mar 14 '19 at 10:38
  • Yep, that did it! Thank you! Aparently I've placed that `default_value ` without actually think too much about it. If you want to post your comment as an answer, I'll be more than happy to accept it! – umbe1987 Mar 14 '19 at 10:44

2 Answers2

2

The mistake here was that you are supplying a default value in the get method call. If publish was not set in the input data, then POST.get('publish', 'default_value') returns default_value. That is a true-y value when you check it via if - so you get both outputs, regardless of which of those buttons was actually clicked to submit the form.

You only want POST.get('publish') here - that either returns a value, if the parameter actually exists - or null/false/whatever (don’t know the details of this in python), if it didn’t.

04FS
  • 5,660
  • 2
  • 10
  • 21
0

this a very effective methods to handle more that one submit buttons in Python Flask

Front-End:

<form method="POST" enctype="multipart/form-data">
          <input type ="submit" name="action" value="upload">

          <input type ="submit" name="action" value="download">

          <input type ="submit" name="action" value="next">
 </form>

Back-End:

if request.form['action'] == 'upload':
    return 'upload pressed'
elif request.form['action'] == 'download':
    return 'download pressed'
elif request.form['action'] == 'next':
    return 'next pressed'
Muhammad Zakaria
  • 1,269
  • 6
  • 14