0

I got a problem with my Code. I have a form for a password reset and an AJAX function which leads the inserted mail to a PHP file. To increase the usability of my form I bound a click on enter to my function. By doing a mouseclick on the button anything works fine. By clicking enter the error-part of my ajax fires, but I don't even get an error message.

I use the same principle on a login form and anything works fine, so I am really confused.

That's my code:

// binding keypress enter
$('#modal-pw-reset').on('keypress', function(e) {
  if(e.keyCode==13) {
    $('#pw-reset-send').trigger('click');
  }
});

// password reset
$("#pw-reset-send").click(function() {
  var email = $("#pw-reset-email").val();
  if (email.length != 0) {
    $.ajax({
      url: 'my php file to check the email',
      method: 'post',
      data: {
        send: 1,
        email: email
      },
      dataType: 'json'
    }).done(function(response) {
      console.log('success');
    }).fail(function(xhr, status, error) {
      console.log(xhr);
    });
  }
});
  • See the error with: `console.log(xhr, status, error);` – freedomn-m Jul 17 '19 at 15:14
  • Make your `#pw-reset-send` element a `button type="submit"` and place it within a `form`, then hook to the `submit` event of that form, not the click of the button. Then you get this behaviour for free, without any extra events needed. – Rory McCrossan Jul 17 '19 at 15:15
  • It doesn't give me good useful information. state is 0 and message is error – mallekarsten Jul 17 '19 at 15:16
  • Potentially related: https://stackoverflow.com/questions/1370021/why-does-forms-with-single-input-field-submit-upon-pressing-enter-key-in-input - single input will submit the form, regardless of any buttons. So could be that it's firing your event handler, but then reloading due to the auto-submit-on-enter. You don't state how many inputs are inside your `
    `
    – freedomn-m Jul 17 '19 at 15:17
  • *"stat[us] is 0"* then your PHP is not returning a valid status and that's where it's falling over. – freedomn-m Jul 17 '19 at 15:18
  • I use input type="button" and it is placed within the form. I use this on other forms to and it works. Only in this case I get an error. – mallekarsten Jul 17 '19 at 15:18
  • I use only two inputs. One for the email and another one for the button. – mallekarsten Jul 17 '19 at 15:22
  • So read the link - it explains what's happening - your form is submitting without your button being clicked. – freedomn-m Jul 17 '19 at 15:30
  • I think it was an issue with the single text input. I added another text input and now it works. Now I know the problem and can build a solution, thank you :) – mallekarsten Jul 17 '19 at 15:31
  • Possible duplicate of [Why does forms with single input field submit upon pressing enter key in input](https://stackoverflow.com/questions/1370021/why-does-forms-with-single-input-field-submit-upon-pressing-enter-key-in-input) – freedomn-m Jul 17 '19 at 15:34

0 Answers0