如何使用jQuery验证器检查数据库中除了特定ID外,用户名和电子邮件是否已存在。

3

我希望在编辑页面中检查用户名和电子邮件是否已存在。因此,需要检查除了特定的那个之外的内容。如何使用jQuery验证器实现呢?

我尝试了以下格式:

jQuery:

<script>
    $(document).ready(function(){         
        $("#frmForm").validate({
            rules: {                
                empemail: {
                           required: true,
                           remote: "../admin/checkEmail.php?data=email"
                },
                username: {
                           required: true,
                           remote: "../admin/checkEmail.php?data=name"
                },              
            },
            messages: {             
                username: {
                               required:"*",
                               remote: "already exists"
                            },
                empemail: {
                               required:"*",
                               remote: "already exists"
                            },              
                }
        });   
    });
</script>

HTML页面

<form  method="post" action="updateEmp.php" id="frmForm">
    <label >Email </label>
    <input type="email" name="empemail" value="<?php echo $row['email'] ?>">    
    <label >Username</label>
    <input type="text" name="username" value="<?php echo $row['username'] ?>">                          
    <button class="btn btn-primary btn-sm" type="submit" >Update</button>
</form>

你使用的是哪个MySQL库? - Shehary
类似的问题已经有答案了。在发布问题之前,请先检查是否有类似的问题存在。http://stackoverflow.com/questions/27620720/jquery-validation-checking-if-value-exists-in-database - Ajinkya Pisal
1个回答

3

在您的远程url中传递特定的id,例如remote: "../admin/checkEmail.php?data=email&id=23",其中23是您的特定id。

然后在您的checkEmail.php文件中,您可以编写查询语句,例如:

"SELECT email FROM table_name WHERE email='".$_GET['email']."' AND id!='".$_GET['id']."'"

同样的方法,您也可以针对用户名进行操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接