Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form returning only first value/tag to python #1

Closed
staminem opened this issue Jun 9, 2021 · 5 comments
Closed

Form returning only first value/tag to python #1

staminem opened this issue Jun 9, 2021 · 5 comments

Comments

@staminem
Copy link

staminem commented Jun 9, 2021

Hi, thanks so much for this simple yet awesome feature. I have one small issue when using it. I want to use it solely for entering "new" tags, so without a "lookup" functionality, within a form in html. I want to pass the data entered to python back-end. However, python only receives the very first value/tag as opposed to the whole list of tags entered. Any idea what may be causing this?
Thanks in advance!
Stefan

@lekoala
Copy link
Owner

lekoala commented Jun 9, 2021

are you sure you have the "multiple" attribute in your select?

i've updated the code pen to post actual data and this is what i got
http://ptsv2.com/t/sfssc-1623244117/d/6332610733146112
so it seems to submit properly

@staminem
Copy link
Author

staminem commented Jun 9, 2021

yes, tried different positions for it as well. this is the code for the html in python:

<div class="form-group row">
<label for="pmids" class="col-sm-2 col-form-label">Pubmed IDs</label>
<div class="col-sm-10">
<select class="form-select" name="pmids" id="pmids" data-allow-new="true" multiple required>
<option selected disabled hidden value="">Enter Pubmed ID and hit Enter/Return...</option>
<option></option>
</select>
<small id="pmidhelp" class="form-text text-muted">Enter any number of Pubmed IDs and hit Enter/Return to add them to the list</small>
<div class="invalid-feedback">Please enter at least one valid Pubmed ID!</div>
</div>
</div>
<br>
<div class="form-group row">
<label for="keywords" class="col-sm-2 col-form-label">Keywords</label>
<div class="col-sm-10">
<select class="form-select" name="keywords" id="keywords" data-allow-new="true" multiple>
<option selected disabled hidden value="">Enter keyword and hit Enter/Return...</option>
<option></option>
</select>
<small id="keywordhelp" class="form-text text-muted">Enter keywords relevant to your project and hit Enter/Return to add them to the list</small>
</div>
</div>

and this is the request bit in python:

@app.route('/process_input',methods=['POST'])
def process_input():
form = request.form
user_email = request.form['user_email']
user_tool = request.form['user_tool']
pmids = request.form['pmids']
keywords = request.form['keywords']
print(pmids)
print(keywords)
return render_template('index.html')

EDIT: very sorry for the messy formatting, can't figure out how to have it display properly

@lekoala
Copy link
Owner

lekoala commented Jun 9, 2021

yes, turns out you need to have name="keywords[]" otherwise it doesn't work

then you should use

keywords= request.POST.getlist("keywords[]")

can you let me know if that's better?

@staminem
Copy link
Author

staminem commented Jun 9, 2021

Yes! Amazing, thank you!
small difference: i had to use
keywords = request.form.getlist('keywords[]')
in flask
thank you so much

@lekoala
Copy link
Owner

lekoala commented Jun 9, 2021

@staminem no worries. i've updated the demo accordingly and added a note in the readme

@lekoala lekoala closed this as completed Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants