0

So I know how to use forms to fetch user input in python flask, however, I want to allow the user to input text in a window popup prompt (I can activate this in javascript), then send this data straight to flask on submission. The problem is that I don't know how to give flask this information since it is in the scope of javascript. May I please know how I can do this?

Thanks in advance!

Edit: MWE code (I hope this has enough detail.)

<!-- students.html -->
<form method="POST", action="/students">
    <button name="remove_student_button" value={{ student.email }} onClick="myFunction()">Remove</button>
</form>

<script>

    function myFunction() {
        let person = prompt("Are you sure you want to remove this entry? Enter the student's email to confirm your action.");
        if (person == null || person == "") {
            // Cancelled. Nothing happens
        } else {
            // Run form method which activates /students.
        }
    }

</script>
# routes.py
@app.route("/students", methods = ["GET", "POST"])
def students():
    # ...
    if "remove_student_button" in request.form:
        email = request.form["remove_student_button"]
        student = Student.query.filter_by(email=email).first()
        print(student.first_name)
        db.session.commit()
        flash("Student removed")
        return redirect(url_for("students", student_removed=True))
    # ...


Kookie
  • 328
  • 4
  • 14
  • Can you share your minimum working example? Lots of options and not 100% clear what you mean. Could you not send the data as JSON to a Flask API, or could you add the fields you need into an existing Flask form and use document.getElementById("nameofid").value = "My value"; in your javascript to set their value so Flask has it on submission. – Johnny John Boy Jun 20 '22 at 10:44
  • @JohnnyJohnBoy Added MWE. I tried not to put in too much clutter so it's easier to read. – Kookie Jun 20 '22 at 13:16
  • I don't see why this question should be closed. The linked question that is apparently "similar" is not in any way similar to this question. I am not currently using the fetch-api thing that is inbuilt into javascript and it doesn't have anything to do with python flask. If you could please kindly tell me here why this question is in any way similar to the one linked at the top, I would greatly appreciate it. – Kookie Jun 20 '22 at 14:09
  • I don't have right to open the question and I didn't close it but I wrote this fiddle for you. https://jsfiddle.net/5sf963bk/1/ I think that should get you most of the way there.. basically you can submit a form with JS and I prevented the default submit. I also switched the logic to explicitly mean the email = the student name or someone could type anything into the box – Johnny John Boy Jun 21 '22 at 09:13

0 Answers0