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
    First thing you need to learn is that [`JavaScript` != `Java`](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – Pshemo Jul 25 '15 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. – jfriend00 Jul 26 '15 at 00:44

2 Answers2

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>');

  • 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 – Facundo A. Díaz Martínez Jul 25 '15 at 19:49
  • You should remove this error message after it shown, because its on static page now – Rostislav Volskyi Jul 25 '15 at 19:50
  • one of the way - to add code just below `.append` `setTimeout(function() { $( ".error" ).fadeOut( "slow" ); },2000);` – Rostislav Volskyi Jul 25 '15 at 19:53
0

You could try something like

$('body').append($errorp);
Borsunho
  • 1,127
  • 7
  • 21