Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

multiform module steps backward on javascript submit!!!


Go to End


2 Posts   1396 Views

Avatar
pinkal

Community Member, 3 Posts

17 September 2014 at 5:52am

Edited: 18/09/2014 9:24am

I am experiencing a strange issue in my multi step form implementation using multiform module. Initially I had two steps and third confirmation steps. That worked perfectly including validation on both steps. Later on I was required to integrate Stripe payment form on 3rd step. So, I created additional step 3 for Stripe payment integration. Please note that on this field I am just adding literal fields as I do not want to store them in anywhere and needed to add 'data' attribute(data-stripe) for each form fields for stripe.js to work.

function getFields(){
Requirements::javascript('https://js.stripe.com/v2/');

$fields = new Fieldlist();
$fields->push(new LiteralField('card','<input type="text" data-stripe="number">'));
$fields->push(new LiteralField('cvc','<input type="text" data-stripe="cvc">'));
$fields->push(new LiteralField('month','<input type="text" data-stripe="exp-month">'));
$fields->push(new LiteralField('year','<input type="text" data-stripe="exp-year">'));

retrun $fields;
}

and on template side I have added little javascript to intercept submit, create token using Stripe.js library and submit form using JavaScript .submit(). as given below

<script>

// This identifies your website in the createToken call below
Stripe.setPublishableKey('pk_test_6pRNASCoBOKtIshFeQd4XMUh');

jQuery(function($) {
$('#FORM_ID').submit(function(event) {
var $form = $(this);

Stripe.card.createToken($form, stripeResponseHandler);

// Prevent the form from submitting with the default action
return false;
});
});

function stripeResponseHandler(status, response) {
var $form = $('#FORM_ID');

if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};
</script>

Strangely form goes step back to step 2 instead of going to last confirm step. But, if I comment out //return false; i.e. I let the form submit normally it goes to last confirm step. But in this case I could not receive stripe token as form does not wait for it to return.
Can some one please tell me if I am doing something wrong? I need help. Thanks.

Avatar
pinkal

Community Member, 3 Posts

17 September 2014 at 6:31pm

Edited: 18/09/2014 9:27am

Well after much effort, I realized that when form submitted normally , 'action_next' input, which is a 'Next' button also gets submitted and form proceeds to next step, but when I submitted form using javascript, 'action_next' was not being submitted hence form stepped backward!!!
But after adding ....

'$form.append($('<input type="hidden" name="action_next" />').val('next'));'

My form goes to next step.