将PhP函数与JQuery集成 - 检查用户是否已经存在于数据库中 [英] Integrate a PhP function with JQuery - Check if User already exists in DB

查看:129
本文介绍了将PhP函数与JQuery集成 - 检查用户是否已经存在于数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php代码来检查用户是否已经存在于我的数据库中,但我希望它先通过jquery。



这样,在新页面上显示错误消息,用户只会收到一条消息,指出用户已经存在并可以更改名称。



看起来像这样:





PHP代码:

 <?php 

$ link = mysqli_connect(localhost, root,,data1)或死(Error。mysqli_error($ link));
$ usercheck =csdcsdsdf;

$ sanitizeduser = filter_var($ usercheck,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_LOW);

$ result = $ link-> query(SELECT username FROM users WHERE username ='。$ sanitizeduser。');

$ row_cnt = $ result-> num_rows;
if($ row_cnt> 0){
echoUser Exists;
} else {
echo找不到用户;
}

?>

我使用预定义的jquery代码(此脚本: http://jqueryvalidation.org/ )'



部分我的jquery代码验证名称:

  names:function(value,element){
return this.optional(element)|| /^\w+$/.test(value);
},

完整的jquery代码:

档案1: http://jsfiddle.net/EjSbd/1/ (脚本.js)

档案2: http:// jsfiddle.net/qM4Uz/1/ (jquery.validate.js)如果您使用的是jQuery验证插件然后这里是解决方案。这是有效的代码。你需要使用遥控器:。这是我现有的代码。这可以帮助你明白。

  $(form#form-join-1)。validate({
规则:{
sponsorID:{
required:true,
minlength:5,
remote:ajax.php
},
slcchild :{
required:true,
remote:{
url:ajax.php,
type:post,
data:{
action:'child-validation',
parent_node:function(){
return $('input#sponsorID').val();
}
}
}
},
消息:{
sponsorID:{
required:Enter a Sponsor ID,
minlength:$ .format(输入至少{0}个字符),
r emote:$ .format({0} is already in use)
},
slcchild:{
required:选择一个节点,
remote:$ .format ({0}不为空)
}
}
});

这是完整的指南。关注此链接。他们有示例代码。 http://imamiscool.wordpress.com/2009/06/29/check-email-availability-using-jquery%E2%80%99s-ajax-part-2-easy-way/

i have a php code to check if a user already exists in my db , but i would like it to go through jquery first .

This way instead of getting an error message on a new page , the user simply gets a message that the user already exists and can change the name.

Looks like this :

PHP CODE:

<?php

$link = mysqli_connect("localhost","root","","data1") or die("Error " . mysqli_error($link));
$usercheck = "csdcsdsdf";

$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);

$result = $link->query("SELECT username FROM users WHERE username = '".$sanitizeduser."' ");

$row_cnt = $result->num_rows;    
if($row_cnt>0){     
    echo "User Exists";
}else{      
    echo "No User found";
}

?>

I'm using a predefined jquery code (this script : http://jqueryvalidation.org/)'

part of my jquery code to validate names :

names: function(value, element) {
            return this.optional(element) || /^\w+$/.test(value);
        },

Full Jquery code :

file 1 : http://jsfiddle.net/EjSbd/1/ (script.js)

file 2 : http://jsfiddle.net/qM4Uz/1/ (jquery.validate.js)

解决方案

If you are using jQuery validation plugin then here is the solution. This is active code. you need to use remote: . This is my existing code. This will help you to get idea.

$("form#form-join-1").validate({
        rules: {
            sponsorID: {
                required: true,
                minlength: 5,
                remote: "ajax.php"
            },
            slcchild: {
                required : true,
                remote : {
                    url: "ajax.php",
                    type: "post",
                    data: {
                        action : 'child-validation',
                        parent_node : function(){
                            return $('input#sponsorID').val();
                        }
                    }
                }
            }
        },
        messages: {
            sponsorID:  {
                required: "Enter a Sponsor ID",
                minlength: $.format("Enter at least {0} characters"),
                remote: $.format("{0} is already in use")
            },
            slcchild:  {
                required: "Select A node",
                remote: $.format("{0} is not empty")
            }
        }
    });

Here is complete guideline. Following this link. They have example code. http://imamiscool.wordpress.com/2009/06/29/check-email-availability-using-jquery%E2%80%99s-ajax-part-2-easy-way/

这篇关于将PhP函数与JQuery集成 - 检查用户是否已经存在于数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆