-1

The problem is that I have two buttons in a form. Additionally, both buttons are running though the same function. How do i force the "Save" button to run though a different function when clicked?

I can post the python code later if necessary.

<form action="/predict" class="form-upload" method="post" enctype="multipart/form-data">
  <h1 class="h3 mb-3 font-weight-normal">Please Upload Image</h1>
  <input autofocus class="form-control" id="image" name="image" required type="file"><br>
  <button type = "submit" name = "predict" formaction="/predict" class="btn btn-lg btn-primary btn-block">Predict</button>
  <button type = "submit" name = "save" formaction = "/add" class="btn btn-lg btn-primary btn-block" >Save</button>
  {% if image_loc %}
  <img id="img" class="mb-4" src="static/{{ image_loc }}" alt="" width="256" height="256">
  <h3 id="nm" class="h3 mb-3 font-weight-normal">Prediction: {{ p }}</h3><br>
  {% endif %}
</form>
Tony
  • 9,672
  • 3
  • 47
  • 75
Alan
  • 1
  • Try adding a different value for both submit buttons. You should then be able to differentiate them. – Bemmu Apr 22 '21 at 12:20
  • It kinda works. If i press the predict button first then the save button no longer runs though the other function. – Alan Apr 22 '21 at 12:34
  • You are getting downvotes because your question is a bit ambiguous, leaving out enough details to be able to answer it. – Bemmu Apr 22 '21 at 14:24
  • Does this answer your question? [Two submit buttons in one form](https://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form) – Tony Apr 22 '21 at 21:20

1 Answers1

0

You can use formaction attribute on the button. It overrides the action attribute of the form.

MDN

  • I get the same result. It works when i reload the page and press the save button first, but not when i press the predict button first – Alan Apr 22 '21 at 13:16