How to get multiple data from dynamic form by FLASK / HTML?

I work on python web app.
In a page i want users to chose their friends to add their organization. But to build that page i prepare data in flask side first. Then i send it to HTML. Then i use for loop in HTML doc to build div blocks ( and add friend information into the blcok ). Then when user chose more than 1 friend, i want to get back data from the form to flask side to use data in my database. But unfortunatelly i cant receive all the values from checkboxes. Coz checkboxes are builded by for loop and all of them names are same.

Here is the flask side that i prepare the data. arkadasList goes to yeni.HTML.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@app.route("/yeni")
def newPlan():
username = session["username"]
cursor = mysql.connection.cursor()
sorgu = "Select friends from users where username = %s"
result = cursor.execute(sorgu,(username,))
if result > 0:
kanka = True
arkadaslar = cursor.fetchone()
arkadasList = list()
arkadaslar = arkadaslar["friends"].split(",")
arkadaslar.pop()
for i in arkadaslar:
sorgu2 = "Select name from users where username = %s"
cursor.execute(sorgu2,(i,))
isim = cursor.fetchone()
isim = isim["name"]
kAdı = i
x = dict(name=isim,username = kAdı)
arkadasList.append(x)
else:
arkadasList = list()
kanka = False
return render_template("yeni.html",arkadasList=arkadasList, kanka=kanka)
</code>
<code>@app.route("/yeni") def newPlan(): username = session["username"] cursor = mysql.connection.cursor() sorgu = "Select friends from users where username = %s" result = cursor.execute(sorgu,(username,)) if result > 0: kanka = True arkadaslar = cursor.fetchone() arkadasList = list() arkadaslar = arkadaslar["friends"].split(",") arkadaslar.pop() for i in arkadaslar: sorgu2 = "Select name from users where username = %s" cursor.execute(sorgu2,(i,)) isim = cursor.fetchone() isim = isim["name"] kAdı = i x = dict(name=isim,username = kAdı) arkadasList.append(x) else: arkadasList = list() kanka = False return render_template("yeni.html",arkadasList=arkadasList, kanka=kanka) </code>
@app.route("/yeni")
def newPlan():
    username = session["username"]
    cursor = mysql.connection.cursor()
    sorgu = "Select friends from users where username = %s"
    result = cursor.execute(sorgu,(username,))
    if result > 0:
        kanka = True
        arkadaslar = cursor.fetchone()
        arkadasList = list()
        arkadaslar = arkadaslar["friends"].split(",")
        arkadaslar.pop()
        for i in arkadaslar:
            sorgu2 = "Select name from users where username = %s"    
            cursor.execute(sorgu2,(i,))
            isim = cursor.fetchone()
            isim = isim["name"]
            kAdı = i
            x = dict(name=isim,username = kAdı)
            arkadasList.append(x)
    else:
        arkadasList = list()
        kanka = False
    return render_template("yeni.html",arkadasList=arkadasList, kanka=kanka)

Here in yeni.HTML doc i use for loop in arkadasList and build a div contains username and name for each loop. ( Also this codes covered by form as well. I did not add here to not make code longer )

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
<div class="arkadasEkle">
<div class="arkadasBaslık">
<h2>Ekleyebileceğiniz Arkadaşlarınız</h2>
<p>Hesaba eklemek istediğiniz arkadaşlarınızı listeden seçiniz</p>
</div>
{% if kanka %} {% for ark in arkadasList %}
<div class="arkadasKutu">
<i class="fa-solid fa-user" style="font-size: 3.7rem"></i>
<section>
<label for="checkbox">{{ark.name}}</label>
<label for="checkbox" style="font-size: 1.7rem"
>{{ark.username}}</label
>
</section>
<input
type="checkbox"
class="checkbox-round"
value="{{ark.username}}"
name="checkbox"
/>
</div>
{% endfor %} {% else %}
<p>oradayım</p>
{% endif %}
</div>
</code>
<code> <div class="arkadasEkle"> <div class="arkadasBaslık"> <h2>Ekleyebileceğiniz Arkadaşlarınız</h2> <p>Hesaba eklemek istediğiniz arkadaşlarınızı listeden seçiniz</p> </div> {% if kanka %} {% for ark in arkadasList %} <div class="arkadasKutu"> <i class="fa-solid fa-user" style="font-size: 3.7rem"></i> <section> <label for="checkbox">{{ark.name}}</label> <label for="checkbox" style="font-size: 1.7rem" >{{ark.username}}</label > </section> <input type="checkbox" class="checkbox-round" value="{{ark.username}}" name="checkbox" /> </div> {% endfor %} {% else %} <p>oradayım</p> {% endif %} </div> </code>

<div class="arkadasEkle">
        <div class="arkadasBaslık">
          <h2>Ekleyebileceğiniz Arkadaşlarınız</h2>
          <p>Hesaba eklemek istediğiniz arkadaşlarınızı listeden seçiniz</p>
        </div>
        {% if kanka %} {% for ark in arkadasList %}
        <div class="arkadasKutu">
          <i class="fa-solid fa-user" style="font-size: 3.7rem"></i>
          <section>
            <label for="checkbox">{{ark.name}}</label>
            <label for="checkbox" style="font-size: 1.7rem"
              >{{ark.username}}</label
            >
          </section>

          <input
            type="checkbox"
            class="checkbox-round"
            value="{{ark.username}}"
            name="checkbox"
          />
        </div>
        {% endfor %} {% else %}
        <p>oradayım</p>
        {% endif %}
      </div>

Here i try to get data back from the FORM. But next pictures you will see, it only gives me the one that i clicked ( checkboxed ) first.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@app.route("/layout", methods = ["GET","POST"])
def layout():
if request.method == "POST":
x = request.form["paymentType"]
a = request.form["hesapName"]
c = request.form["hesapKat"]
y = request.form["checkbox"]
b = x+a+c+"----"+y
return render_template("layout.html", b=b)
else:
return render_template("layout.html")
</code>
<code>@app.route("/layout", methods = ["GET","POST"]) def layout(): if request.method == "POST": x = request.form["paymentType"] a = request.form["hesapName"] c = request.form["hesapKat"] y = request.form["checkbox"] b = x+a+c+"----"+y return render_template("layout.html", b=b) else: return render_template("layout.html") </code>
@app.route("/layout", methods = ["GET","POST"])
def layout():
    if request.method == "POST":
        x = request.form["paymentType"]
        a = request.form["hesapName"]
        c = request.form["hesapKat"]
        y = request.form["checkbox"]
        b = x+a+c+"----"+y
        return render_template("layout.html", b=b)
    else:

         return render_template("layout.html")

yeni.HTML
layout.HTML after post request from yeni.html

I want to receive the usernames that checkboxed

New contributor

Anıl Demirelli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật