使用request.form接收从html到python的多行数据?

时间:2019-07-15 19:46:40

标签: python html flask

我正在尝试使用POST方法将数据从html发送到python,下面是python的1行有效代码

def InsertData():
    if request.method == 'POST':
        conn = pyodbc.connect('DRIVER={SQL Server};''SERVER=.;''DATABASE=Store;''TRUSTED_CONNECTION=yes;')
        try:
            unit = request.form['unit']
            head = request.form['head']
            headname = request.form['headname']
            vocabid = request.form['vocabid']
            cursor = conn.cursor()
            cursor.execute("SELECT MAX ([HEAD_ID])FROM [Store].[dbo].[Head] ")
            row = cursor.fetchall()
            id = row[0]
            temp = str(id)
            lent = len(temp)
            r_id = temp[2:lent-4]
            rr_id = int(r_id) + 1
            ctypes.windll.user32.MessageBoxW(0, str(rr_id), "Test Box Ali Azeem", 1)
            type= request.form['type']
            cursor.execute("insert into [Store].[dbo].[Head] values ( ?,?,?,?,?,? )",(rr_id,unit,head,type,headname,vocabid) )
            conn.commit()
            msg = "Record successfully added"
        except:
            conn.rollback()
            msg = "error in insert operation"
        finally:
            conn.close()
    return render_template("index.html")

但是我有动态字段,因为我可以添加多行,然后如何处理呢?现在,字段名称为type0,unit0,head0 ..... type1,unit1,head1 ....依此类推... HTML和js代码在下面

</form>

        <!-- project team & activity start -->
 <form method="POST" id="form111" action="\insertdata">       
 <div id="cont"></div>
 <input type="button" class="btn bg-primary" id="addrow" value="Add New Row" onclick="addRow()"/>
 <button type="submit" class="btn bg-primary">Submit</button>
 </form>

在javascript中我要添加多行。

0 个答案:

没有答案