1

I have a form (for password reset) that first validates that the input is a correct email, then it should look for that email's existence in my user database, and finally reset the password and email the user the new one.

I'm able to do everything, but I want to have it work without page refresh. Typically I would do this all in PHP, and if there is no such email entry in my database, it would respond back after a page refresh. The thing is, I want the file that this page is using as a POST to be able to send back data to the page without a refresh, showing an error or not.

I'm going to be using jQuery $.post(). Any help?

Here's my jQuery post code:

 $.post('php/recoverPost.php', $('#recoverPost').serialize(), function(){
                $('#recoverPost').hide();
                $('#success').show();
            });

2 Answers 2

2

You could do something like this:

$('#registerform').submit(function() {
    e.preventDefault();
    $.ajax({
        'url': 'posturl.php',
        'type': 'POST',
        'data': {
            'email': $('#email').val(),
            'username': $('#username').val()
        },
        'success':function(data) {
            if(data == 'success') {
                alert('did it!');
            } else {
                alert('I failed!');
            }
         }
    });
});

if you make posturl.php echo 'success' if the e-mail doesn't exist this will work.

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

2 Comments

That's exactly what I want to do but I'm trying to use $.post() instead of $.ajax().
The code posted by @teuneboon will do the same as a $.post(), it's just a longer way of doing it. (I actually prefer doing it that way myself). See: api.jquery.com/jQuery.post
0
$("#form").submit(function(event) {
    event.preventDefault(); 
    $.post( url,  ,$('#recoverPost').serialize(),
      function( data ) {
          $('#recoverPost').hide();
          $('#success').show();
      }
    );
  });

Comments

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.