0

First of all , I have no idea of ​​javascript, I got this code from a tutorial. I am developing a website in ruby , and to do that I need to make a form of payment. I'm currently using the API Mango. I have the following code:

<form id="form" action="/pay" method="POST">

  <fieldset>

    <div>
      <label for="ccv">Código de seguridad</label>
      <input type="text" id="ccv" required>
    </div>

  </fieldset>

  <input type="submit" value="Pagar ahora!">

</form>
<div id="errores">

</div>


<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://js.getmango.com/v1/mango.js"></script>

<script>
  var PUBLIC_API_KEY = 'public_test_u3wbj4jctik1k2u8qtnajhvn82h590ue';
  Mango.setPublicKey(PUBLIC_API_KEY);
  var submission = false;
  var $form = $('#form');
  $form.on('submit', function(event) {
    if (submission) {
      return false;
    }
    submission = true;
    var cardInfo = {
      'ccv': $('#ccv').val()
    };
    Mango.token.create(cardInfo, handleResponse);
    return false;
  });
  function handleResponse(err, data) {
    submission = false;
    //Here I put an error message that's displayed in html

    if (err) {
      ...  
    }
    var token = data.uid;
    var $hidden = $('<input type="hidden" name="token">');
    $hidden.val(token);
    $form.append($hidden);
    $form[0].submit();
  }
</script>

How I can capture that error and show it in html ?

2
  • 2
    First thing you need to learn is that JavaScript != Java Commented Jul 25, 2015 at 19:14
  • Next time use a title that describes something about what you're trying to do rather than a very generic title that won't necessarily attract people that know about your question. Commented Jul 26, 2015 at 0:44

2 Answers 2

1
  function handleResponse(err, data) {
    submission = false;
    //Here I put an error message that's displayed in html

    if (err) {
      ...  
    }
    var token = data.uid;
    var $hidden = $('<input type="hidden" name="token">');
    $hidden.val(token);
    $form.append($hidden);
    $form[0].submit();
  }

this function - is an event handler for Response,obviously. First param is error and if this will be passed your if(err) code block will be executed. As i see - you use JQuery, so in this place you can Insert some code, which will show error into your form. For example $('form').append('<div class="error">Some error occurs</div>');

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You! The code works . The only problem is that the error is shown twice : Some error occurs Some error occurs And when I send the form with errors again , it appears 4 times : Some error occurs Some error occurs Some error occurs Some error occurs
You should remove this error message after it shown, because its on static page now
one of the way - to add code just below .append setTimeout(function() { $( ".error" ).fadeOut( "slow" ); },2000);
0

You could try something like

$('body').append($errorp);

1 Comment

Sorry , I forgot to remove that piece of code. I edited the post , please watch it again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.