I am making a website that has a submit button and multiple input fields in different divs. I am using Flask to make the website, and I want to log what is submitted from those input fields into separate variables, to eventually be stored in a database. My problem is that whenever I press the submit button, it only works with one text box, the one closest to it in the HTML.
This is the Flask code for getting the text from the input box:
@app.route('/', methods=["GET", "POST"])
def home():
if request.method == "POST":
name = request.form.get("name")
notes = request.form.get("notes")
print(notes)
return render_template("messaging.html")
Here's the HTML for the actual inputs and submissions:
<div >
<div >
<input type="text" id="name" name="name" placeholder="Name" autocomplete="off">
</div>
<div id="user-input">
<input type="text" name="notes" placeholder="Notes" autocomplete="off">
</div>
<button type="submit" >Send</button>
</div>
It only works with the notes input box, not the name.
If you can help that would be great, sorry if the code isn't very good I'm new to HTML and Flask. Thanks!
CodePudding user response:
any time you use button with type "submit", must use form. and button can submit your form.
CodePudding user response:
Watching some HTML tutorials won't be a bad idea if you faced a challenge in forms.
<form id="form1" action="/yourRoute" method="POST"></form>
<div >
<div >
<input type="text" id="name" name="name" placeholder="Name" autocomplete="off">
</div>
<div id="user-input">
<input type="text" name="notes" placeholder="Notes" autocomplete="off">
</div>
<button type="submit" >Send</button>
</div>
</form>