web2py using "foreign" form

34 views
Skip to first unread message

Scott Hunter

unread,
Mar 23, 2017, 4:52:56 AM3/23/17
to web2py-users
Suppose I have a form whose contents are generated by a script, and I do not know what those contents are; in particular, I may not know what the fields are within that form, but I certainly have no control over them.  I can specify the controller that I want that form to submit to (via the `action` attribute).  Is there a way, within that controller, I can get the content of that submission (what would show up in `request.post_vars` for a web2py-generated form)?  Does it help if I know the names of these fields?

- Scott

Anthony

unread,
Mar 23, 2017, 9:28:50 AM3/23/17
to web2py-users
If the form is submitted via a POST request, everything will be in request.post_vars (request.post_vars is not specific to web2py-generated forms -- it is simply where posted data are made available).

Anthony

Scott Hunter

unread,
Mar 23, 2017, 10:20:29 AM3/23/17
to web2py-users
That's what I would have thought, but if I have the following in my view:

<form action="{{=URL(c='pay',f='charge',args=request.args)}}" method="POST">
 
<script
   
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
   
data-email="{{=auth.user.email}}"
   
data-key="{{=pk}}"
   
data-amount="{{=amount}}"
   
data-name="UKI TMS"
   
data-description="{{=description}}"
   
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
   
data-panel-label="{{=label}}"
   
data-locale="auto">
 
</script>
</form>

and the following in my `pay` controller:

def charge():
   
return DIV("%r"%request,_class="well")



the displayed value of `request._post_vars` is None.  Same thing if I don't specify `args` in the action URL, or leave the action URL empty (and display `request.env` from the appropriate controller).

Poking around the DOM, I see that there is *another* form inside of mine, and this is the form which contains the "submit" button (along with all of the input fields).

Even more confused now,
Scott

Anthony

unread,
Mar 23, 2017, 6:08:59 PM3/23/17
to web2py-users
On Thursday, March 23, 2017 at 10:20:29 AM UTC-4, Scott Hunter wrote:
That's what I would have thought, but if I have the following in my view:

<form action="{{=URL(c='pay',f='charge',args=request.args)}}" method="POST">
 
<script
   
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
   
data-email="{{=auth.user.email}}"
   
data-key="{{=pk}}"
   
data-amount="{{=amount}}"
   
data-name="UKI TMS"
   
data-description="{{=description}}"
   
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
   
data-panel-label="{{=label}}"
   
data-locale="auto">
 
</script>
</form>

and the following in my `pay` controller:

def charge():
   
return DIV("%r"%request,_class="well")



the displayed value of `request._post_vars` is None.  Same thing if I don't specify `args` in the action URL, or leave the action URL empty (and display `request.env` from the appropriate controller).

That's not the way to check for posted variables. request._post_vars is populated lazily the first time request.post_vars (or request.vars) is accessed. You should simply check directly for request.post_vars.stripeToken (which is the only field that will be posted to the controller).
 
Poking around the DOM, I see that there is *another* form inside of mine, and this is the form which contains the "submit" button (along with all of the input fields).

Notice in your code you are loading a Stripe Javascript script. Once loaded, that script generates the form you are seeing in the DOM. That form is what the user fills out, and it is submitted directly to Stripe, not to your web2py server. Stripe then returns the "stripeToken" to the browser, adds it to a hidden field within your form, and submits that form to the web2py controller.

Anthony

Scott Hunter

unread,
Mar 23, 2017, 8:20:54 PM3/23/17
to web2py-users
There is no `request.post_vars`, and thus no `request.post_vars.stripeToken`, when the controller the form was submitted to executes.

I cannot see any such hidden field in the DOM, but there not being one would explain why it isn't getting back to me.

Further poking around, I found a section (not displayed) of class stripeErrorMessage, the contents of which says 

    Sorry, there was a problem loading Checkout.

    If this persists, please try a different browser.
  
But I'm thinking that is there to be displayed IF there is a problem, as the form comes up fine and responds to the credit card number appropriately (except for giving me back the token).
Reply all
Reply to author
Forward
0 new messages