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