Popup中的表单验证

时间:2014-11-27 11:11:57

标签: javascript php jquery ajax

在弹出窗口中处理LOGIN表单..当表单提交时,它返回验证并显示错误但我想要与弹出的数据库进行比较并显示错误消息,如果登录名和密码不匹配,我有尝试使用ajax获取与DB相比的电子邮件和密码..

JS:

function login_validation(){ 
    //worked with some validations like empty string and email filter...they are working fine displaying errors                                     
    $(document).ready(function(){
        $("#upwd").on("focusout",function(){
            var uemail = $("#uemail").val();
            var upwd = $("#upwd").val();

            $.ajax({
                type: "POST",
                url: "checkl_login.php",
                data: 'uemail='+uemail+'&upwd='+upwd,
                success: function(msg){
                $('#status').html(msg);
                    return false;   
                } 
            });
        }); 
    });
}

HTML:

<form name="f" method="POST"  action='' onsubmit="return login_validation()">

<p><input type="text"  placeholder="Email"  name="uemail"  id="uemail"  > </p><span id="uemailerror"></span>


<p><input type="password"  placeholder="password"  name="upwd"  id="upwd" ><span id="upassworderror"></span><span id="status"></span>

<!--submit button.. -->

PHP ,checkl_login.php:

ob_start();

session_start();

include('dbconnect.php'); 

include('gettime_diff.php');  

mysql_select_db("hccommunity") or die(mysql_error()); 

if($_POST['uemail'] && $_POST['upwd']){

    $loginemail = mysql_escape_string($_POST['uemail']);  

    $loginpwd = mysql_escape_string($_POST['upwd']); 

    $search = mysql_query("SELECT * FROM user WHERE email='".$loginemail."' AND password='". mysql_escape_string(md5($loginpwd)) ."'") or die(mysql_error());   

    $match  = mysql_num_rows($search);  

    if($match > 0){  
        echo "OK";
    }

    else{
        echo "ERROR";
    }
}

表单未显示“错误”或“确定”消息....

1 个答案:

答案 0 :(得分:0)

使用以下代码替换/删除您的login_validation()函数。

$(document).ready(function(){
    $("form").on("submit",function(e){

        var uemail = $("#uemail").val();
        var upwd = $("#upwd").val();
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "checkl_login.php",
            data: 'uemail='+uemail+'&upwd='+upwd,
            success: function(msg){
            $('#status').html(msg);
                return false;   
            } 
        });
    }); 
});

并用此

替换form标记

<form name="f" method="POST" action=''>