结合PHP和jQuery验证

结合PHP和jQuery验证

问题描述:

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();
            });

我有一个表单(用于密码重置),首先验证输入是否是正确的电子邮件,然后它应该看起来 该电子邮件存在于我的用户数据库中,最后重置密码并通过电子邮件向用户发送新密码。 p>

我能够做所有事情,但我想让它在没有页面刷新的情况下工作。 通常情况下,我会用PHP完成所有操作,如果我的数据库中没有这样的电子邮件条目,它会在页面刷新后回复。 问题是,我希望此页面用作POST的文件能够在不刷新的情况下将数据发送回页面,显示错误与否。 p>

我是 将使用jQuery $ .post() code>。 有什么帮助吗? p>

这是我的jQuery邮政编码: p>

  $ .post('php / recoverPost.php',$('#  recoverPost')。serialize(),function(){
 $('#recoverPost')。hide(); 
 $('#success')。show(); 
}); 
  code  >  pre> 
  div>

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.

$("#form").submit(function(event) {
    event.preventDefault(); 
    $.post( url,  ,$('#recoverPost').serialize(),
      function( data ) {
          $('#recoverPost').hide();
          $('#success').show();
      }
    );
  });