2

Okay so I followed stripes tutorial for creating a custom form. And my code is as follows for the javascript. Now my credit card fields are part of a larger form and not just those fields. it's ID is payment-form as you would think it would be in the code below.

My issue is that when the code runs everything works up until this line: $form.get(0).submit(); where I receive this error: Uncaught TypeError: object is not a function I tried removing some stuff and making it: $form.submit but that resulted in a infinite loop.

So what do I need to do to fix this problem? What is causing this problem?

My Code:

<script type="text/javascript">
Stripe.setPublishableKey('<?php echo $key; ?>');
var stripeResponseHandler = function(status, response) {
  var $form = $('#payment-form');

  if (response.error) {
    // Show the errors on the form
    $form.find('.payment-errors').text(response.error.message);
    $form.find('#submit').prop('disabled', false);
  } else {
    // token contains id, last4, and card type
    var token = response.id;
    // Insert the token into the form so it gets submitted to the server
    $form.append($('<input type="text" name="stripeToken" />').val(token));
    $form.find('[data-stripe]').val('');
    // and re-submit
    $form.get(0).submit();
  }
};
jQuery(function($) {
  $('#payment-form').submit(function(event) {
    var $form = $(this);

    // Disable the submit button to prevent repeated clicks
    $form.find('#submit').prop('disabled', true);

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

    // Prevent the form from submitting with the default action
    return false;
  });
});
</script>
user2948950
  • 137
  • 1
  • 4
  • 13

1 Answers1

4

Change the id/name of the submit button to something else..aka name="btnSubmit"

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Hmmm... That did it.... but why? – user2948950 May 29 '14 at 21:33
  • because you overrode the submit method with the name `document.formName.elementName` and `document.formName.methodName`. So when you have a method and element name with the same value, what should the browser do? – epascarello May 29 '14 at 21:38