Fabian Omobono
Fabian Omobono

Reputation: 101

getting the Value of and HTML element

I am designing a web app for my final project in CS50. I need to connect two users as friends(like FB). In order to connect the users in my database I need to get the Value of a generated HTML element. I am using flask

I tried to get the Value of the element with: request.form.get('value') request.args.get('value')

both return none

<div>
   <ol>
  {% for x in range(rows|length) %}

  <form  value='{{loop.index}}' action="connect_friends" method="POST">

  <li name="friend" value='{{loop.index}}'> <a>{{rows[loop.index -1][1]}}  {{rows[loop.index -1][2]}}</a> add to  
    <select name='groups'>
      {% for x in range(5) %}
      <option value='group {{loop.index}}'>Group {{loop.index}}</option>

      {% endfor %}
    </select>
    <button class="btn btn-primary" type='submit'>Add</button>
    <br>
    <br>
  </form>

  {% endfor %}
   </ol>
@app.route("/connect_friends", methods=["POST"])
def connect_friends():
    if request.method == "POST":
        # getting the group number-- this works as expected
        group_number = request.form.get('groups')

        # getting the value of the HTML element this returns NONE
        friend = request.form.get('friend')

        # printing to check 
        print(group_number)
        print(friend)

        return apology(f"{group_number}")

    else:
        return apology("something went wrong", 403)

the expected output is that friend is not NONE

Upvotes: 0

Views: 42

Answers (1)

Sam Creamer
Sam Creamer

Reputation: 5361

It's because you're using an li tag which will not be passed in with the request.

You can try this:

<input style="display:none;" name="friend" value="{{loop.index}}" />

Put that input somewhere within your <form> ... </form> area and you should get the value.

Upvotes: 1

Related Questions