3

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?

ZiGiUs
  • 394
  • 4
  • 15
  • 1
    are there any input fields in this form? You should be checking for that rather than the name of the form. – Fosco May 24 '11 at 16:00
  • what do you mean by checking for input fields in the form? can you please add an example? – ZiGiUs May 24 '11 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 May 24 '11 at 17:39
  • OMG! thank you very much! adding an input field to the form did the trick! – ZiGiUs May 24 '11 at 17:59
  • I moved my comments to an answer so you can accept it. – Fosco May 24 '11 at 18:01

3 Answers3

3

here 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>`
ZiGiUs
  • 394
  • 4
  • 15
2

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.

Adam Bergmark
  • 7,316
  • 3
  • 20
  • 23
  • Note that `myform` should ideally be the id of the form, and not the name (although both work, it's nicer to work with id's since forms shouldn't have names). – Adam Bergmark May 24 '11 at 16:02
  • thanks for the quick reply! upon clicking the href "button" a post request is sent to the server but the post is sent without any data in it and for this reason our check still fails. we have multiple submit buttons in the page so we have to send the name of the form to our server in order to distinguish between the forms. but using just the id works as well so thank you :) any other ideas? – ZiGiUs May 24 '11 at 17:29
  • Is the button inside the form? As for the form name in that case, you can replace it with an input hidden. – Adam Bergmark May 24 '11 at 17:40
0

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:

Fosco
  • 38,138
  • 7
  • 87
  • 101