Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use many submit buttons in the same form

Tags:

python

html

flask

I cannot access all the buttons from the frame. It is works only with the Histogram button.Here is my form which I want to access it in the Post method.

 <form id="package_form" action="" method="post">
      <div class="panel-body">
          <input type ="submit" name="Download" value="Download">
      </div>
      <div class="panel-body">
          <input type ="submit" name="Histogram" value="Histogram">
      </div>
      <div class="panel-body">
           <input type ="submit" name="Search" value="Search">
      </div>

 </form>

Here is my python code.

 if request.method == 'GET':
        return render_template("preview.html", link=link1)
    elif request.method == 'POST':
        if request.form['Histogram'] == 'Histogram':
            gray_img = cv2.imread(link2,cv2.IMREAD_GRAYSCALE)
            cv2.imshow('GoldenGate', gray_img)
            hist = cv2.calcHist([gray_img], [0], None, [256], [0, 256])
            plt.hist(gray_img.ravel(), 256, [0, 256])
            plt.xlabel('Pixel Intensity Values')
            plt.ylabel('Frequency')
            plt.title('Histogram for gray scale picture')
            plt.show()
            return render_template("preview.html", link=link1)

        elif request.form.get['Download'] == 'Download':
            response = make_response(link2)
            response.headers["Content-Disposition"] = "attachment; filename=link.txt"
            return response
        elif request.form.get['Search'] == 'Search':
            return link1

What I am doing wrong?

like image 469
Andreea Avatar asked May 05 '17 18:05

Andreea


People also ask

Can we have multiple submit buttons in one form?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How can I use multiple submit buttons in an HTML form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Can you have multiple submit buttons in a form MVC?

There are multiple ways to use multiple submit buttons on a single view in mvc. Here you will learn about the most 3 popular ways to use multiple submit buttons on a single view. # First way (using a different name on all submit buttons)

How can we use multiple submit button in single form in PHP?

Having multiple submit buttons and handling them through PHP is just a matter of checking the the name of the button with the corresponding value of the button using conditional statements. In this article I'll use both elseif ladder and switch case statement in PHP to handle multiple submit buttons in a form.


2 Answers

It won't work the way you've written it. Only the submit button you send will be included in request.form, you'll get an error if you try to use the name of one of the other buttons.

Also, request.form.get is a function, not a dictionary. You can use request.form.get("Histogram") -- this will return the value of the Histogram button if it was used, otherwise it will return None.

Instead of giving the buttons different names, use the same name but different values.

<form id="package_form" action="" method="post">
      <div class="panel-body">
          <input type ="submit" name="action" value="Download">
      </div>
      <div class="panel-body">
          <input type ="submit" name="action" value="Histogram">
      </div>
      <div class="panel-body">
          <input type ="submit" name="action" value="Search">
      </div>

 </form>

Then your Python code can be:

if request.form['action'] == 'Download':
    ...
elif request.form['action'] == 'Histogram':
    ...
elif request.form['action'] == 'Search':
    ...
else:
    ... // Report bad parameter
like image 104
Barmar Avatar answered Oct 22 '22 02:10

Barmar


Although using the same name for different submits can work (as suggested in the previous answer), it can be not so convenient for i18n.

You can still use different names, but you should check handle them differently:

if 'Download' in request.form:
    ...
elif 'Histogram' in request.form:
    ...
elif 'Search' in request.form:
    ...
else:
    ... // Report bad parameter
like image 39
avtomaton Avatar answered Oct 22 '22 01:10

avtomaton