Send a form array input to Flask
The following example shows, how send HTML form array input to Flask using jQuery Ajax.
The following HTML form has array input elements.
<input type='text' name='contact[]' class='cnt'> <input type='text' name='contact[]' class='cnt'> <input type='text' name='contact[]' class='cnt'> <input type='text' name='contact[]' class='cnt'> <input type='text' name='contact[]' class='cnt'> <input type='button' id='saveBtn' value='Click'>
We have to send form inputs to Flask using jQuery Ajax
$("#saveBtn").click(function(){ var arr=[]; $(".cnt").each(function(){ arr.push($(this).val()); }); $.ajax({ url:'{{url_for("array_post")}}', type:'post', data:{contacts:arr}, success:function(res){} }); });
Receive form post data in Flask
@app.route("/array_post",methods=['GET','POST']) def array_post(): if request.method=='POST': a=request.form.getlist("contacts[]") for x in a: print(x) return ""