I am working on a Flask application where I have one page with multiple buttons, and for each of those buttons when clicked I wanted a "confirm - are you sure?" popup to come up, before going on to another view function which takes care of running a job depending on the button. I am stuck trying to find a way to pass a variable from button via modal to run view function.
Here is what I have tried so far:
app.py:
@app.route('/buttons', methods=['GET', 'POST'])
def buttons():
return render_template("buttons.html")
@app.route('/run/<cmd>', methods=['GET', 'POST'])
def run(cmd):
return render_template_string('''
cmd = {{ cmd }}<br>
''', cmd=cmd)
buttons.html:
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<meta charset="utf-8">
<script type="text/javascript">
$(document).on("click", ".open-modal", function () {
var mycmd = $(this).data('id');
$(".modal-body #cmd").val( mycmd );
$("#confirm-modal").modal("show");
<script>
</head>
<body>
<form method="post" action="/">
<button type="button" class="open-modal" data-toggle="modal" data-id="Test" data-target="#confirm_modal">Test</button>
<button type="button" class="open-modal" data-toggle="modal" data-id="Test2" data-target="#confirm_modal">Test2</button>
</div>
</form>
<!-- Modal for Pop Up-->
<div class="modal" tabindex="-1" role="dialog" id="confirm-modal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Confirm - Are you sure?</p>
<input type="hidden" name="cmd" id="cmd" value=""/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<form action="{{ url_for('run', cmd=cmd) }}" method="POST">
<input class="btn btn-secondary" type="submit" name="submit_button" value="Confirm">
</div>
</div>
</div>
</div>
</body>
Prior to an attempt to add a modal, I was using a submit buttons in a form within button.html where I would catch a value of a button in buttons function and pass a variable to a session and then get it in run function. However modal requires a button type="button" for a trigger which doesn't send anything back to a form. I also looked into using and somehow passing a hidden input value or serving modal from a different HTML page.
Any suggestion on the best way to make this work? Previously I had a confirm delete modal where I just wrapped modal's confirm button in a form with url_for working so I figured this wouldn't be so complicated. Unfortunately I am not too well versed in Java script. Any help is much appreciated!