admin管理员组文章数量:1326523
ok so have this snippet of code -
<form id="myform" name="myform" action="." method="post">
<a href="javascript: document.forms['myform'].submit();" name="myform">Submit
</a>
</form>
i am submitting a form using an href and some javascript to my django server. on the server i check the post request using the following code -
if 'myform' in request.POST:
'''handle the form submition'''
return True
return False
this returns false. any ideas why?
ok so have this snippet of code -
<form id="myform" name="myform" action="." method="post">
<a href="javascript: document.forms['myform'].submit();" name="myform">Submit
</a>
</form>
i am submitting a form using an href and some javascript to my django server. on the server i check the post request using the following code -
if 'myform' in request.POST:
'''handle the form submition'''
return True
return False
this returns false. any ideas why?
Share Improve this question asked May 24, 2011 at 15:58 ZiGiUsZiGiUs 4041 gold badge5 silver badges15 bronze badges 5- 1 are there any input fields in this form? You should be checking for that rather than the name of the form. – Fosco Commented May 24, 2011 at 16:00
- what do you mean by checking for input fields in the form? can you please add an example? – ZiGiUs Commented May 24, 2011 at 17:35
-
like, actual data being submitted... The form itself is nothing, so what data is being posted? i.e. a text input field named someData.
if 'someData' in request.POST:
– Fosco Commented May 24, 2011 at 17:39 - OMG! thank you very much! adding an input field to the form did the trick! – ZiGiUs Commented May 24, 2011 at 17:59
- I moved my ments to an answer so you can accept it. – Fosco Commented May 24, 2011 at 18:01
3 Answers
Reset to default 3here is the solution i used to solve my problem - (thank you very much fosco and adam!)
<form id="my_form" action="." method="post">
<a href="#" onclick="document.forms['my_form'].submit();">Call Form</a>
<input type="checkbox" name="call_form" checked style="visibility:hidden"><br>
<input type="submit" value="create form" style="visibility:hidden" />
</form>`
I assume "using an href" means you click a link programatically? Links always send GET requests so that's why it fails. You can submit the entire form with JS using document.forms.myform.submit();
and that will send it with POST since that's the method you specified in the form.
Are there any input fields in this form? You should be checking for that rather than the name of the form. The form itself is nothing, so what data is being posted? i.e. a text input field named someData. if 'someData' in request.POST:
本文标签: pythonsubmitting a django form using javascriptStack Overflow
版权声明:本文标题:python - submitting a django form using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742198848a2431582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论