Passing Variables Between Javascript and Python -
i attempting pass value of integer within javascript function server side python script. have tried find way pass value directly javascript python have not yet been successful, instead have tried create hidden element contains value of int within html form javascript function. using action 'post' python bottle framework have tried copy value python script. however, int being processed being of nonetype, rather int, , cannot use within processing script. part of js function creates element int named instance follows
function newitem(){ instance++; var oldinput = document.getelementbyid("iteminfo"); var parent = oldinput.parentnode; var newdiv = document.createelement("div"); var item = document.createelement("input"); var qty = document.createelement("input"); var color = document.createelement("input"); var count = document.createelement("hidden"); item.name = "item" + instance; qty.name = "qty" + instance; color.name = "color" + instance; count.value = instance; newdiv.appendchild(item); newdiv.appendchild(qty); newdiv.appendchild(color); newdiv.appendchild(count);
the html form 'post' method
<form method ="post" class="form" action = "/newguest" method = 'post'> name: <input type="text" name="name"/> <p>item: <input type="text" name="item"/> qty: <input type="text" name="qty"/> color: <input type="text" name="color"/></p> <div class="iteminfo" id="iteminfo"></div> <input type ="button" value="add item" onclick="newitem();"/> <p>phone: <input type="text" name="phone"/> email: <input type="text" name="email"/> artwork: <input type="file" name="file"/> <p>quote: <input type="text" name="quote"/></p> </p> <p>notes: <textarea cols="40" rows="10"></textarea> </p> <input type="submit" value='add order'/> </form>
and python script on server side
@bottle.route('/newguest', method = 'post') def insert_newguest(): name = bottle.request.forms.get("name") email = bottle.request.forms.get("email") item = bottle.request.forms.get("item") qty = bottle.request.forms.get("qty") color = bottle.request.forms.get("color") count = bottle.request.forms.get(count) itemdict = dict() qtydict = dict() colordict = dict() num in range(1, count): itemkey = "item" + str(num) qtykey = "qyt" + str(num) colorkey = "color" + str(num) itemdict[itemkey]= bottle.request.forms.get("item"+str(num)) qtydict[qtykey] = bottle.request.forms.get("qty"+str(num)) colordict[colorkey] = bottle.request.forms.get("color"+str(num))
when attempting add information 'post' method, receive following error:
you getting message because hidden field hasn't been created correctly.
firstly, can't see adding
newdiv
dom in code above.secondly - html form have provided hard coded? if why hardcoding form , creating fields again in javascript? seems bit weird.
thirdly , importantly, hidden field
<input>
, need replacevar count = document.createelement("hidden");
with:
var count = document.createelement("input"); count.setattribute('type', 'hidden'); count.setattribute('name', 'count'); count.setattribute('value', my_count_variable);
see this answer , example jsfiddle. when submit form count
field should populated in python script.
as aside, kind of request handled ajax. idea same don't need refresh browser send count python server. can see example of how without jquery here.
Comments
Post a Comment