導航:首頁 > 編程語言 > phpjquery驗證用戶名是否存在

phpjquery驗證用戶名是否存在

發布時間:2022-04-17 12:46:24

1. php如何用jquery ajax做登錄注冊的驗證

這個非空驗證和密碼不能重復不需要Ajax吧,jquery不是就能直接實現了。如果要驗證用戶名是否已經存在倒是要先資料庫查詢下要用Ajax,一般是焦點離開文本框觸發事件調用function,獲取表單值,Ajax用post或get傳值給後台,然後php文件接收到這值進行處理,資料庫查詢該用戶名是否已經存在。然後用json數據格式返回數據,jquery回調函數接收到數據後進行處理,改變樣式或者文本,顯示用戶名已存在或者用戶名可使用。

2. jquery 判斷用戶名是否存在

$("#name").blur(function(){
$.post("checkName.asp",
{
name:$(this).val()
},
function(data,status){
if(data=='0'){
$(".state").html("可以注冊");
}
else{$(".state").html("已經存在");}
});
});


上面是JQUERY代碼,校驗頁面你不存在就輸出0,存在就輸出1,我用ASP整了個示例:

<%
dimconn
setconn=server.CreateObject("adodb.connection")
conn.open"driver={microsoftaccessdriver(*.mdb)};dbq="&server.MapPath("db.mdb")

setrs=server.CreateObject("Adodb.Recordset")
sql="select*fromt_AdminwhereadminName='"&request("name")&"'"
rs.opensql,conn

ifrs.eoforrs.bofthen
response.Write"0"
else
response.Write"1"
endif

rs.close()
setrs=nothing
%>

3. php 檢測資料庫中用戶名是否存在

樓上的sql語句要加上一些才行。

$sql="select*from表where用戶名='{$_POST['user']}'and密碼='{$_POST['pass']}'";
$row=mysql_query($sql);
if(!empty($row)){
echo"<script>alert('用戶名存在,不可以注冊')</script>";
}else{
echo"<script>alert('用戶名不存在,可以注冊')</script>";
}

4. php注冊的時候怎麼驗證用戶名是否存在

1<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""
002
003<head>
004<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
005<metahttp-equiv="X-UA-Compatible"content="IE=EmulateIE7"/>
006<linkrel="stylesheet"type="text/css"href="css/int.css"/>
007<scripttype="text/javascript"src="js/func.js"></script>
008<styletype="text/css">
009td{
010height:30px;
011vertical-align:middle;
012align:center;
013}
014#myText{
015width:600px;
016}
017</style>
018<title>注冊頁面http://yige.org</title>
019</head>
020
021<body>
022
023<?php
024error_reporting(0);
025//不讓PHP報告有錯語發生。如果不關閉好有類似這的錯語Warning:preg_match()關閉就不出現了
026session_start();
027header("Cache-control:private");
028
029$conn=@mysql_connect("localhost","root","")ordie("資料庫連接錯誤");
030mysql_select_db("bbs",$conn);
031mysql_query("setnamesutf8");
032
033if($_POST['submit'])
034{
035$username=$_POST["username"];
036
037$sql="selectuserNamefromuser_infowhereuserName='$username'";
038//echo$sql;
039
040$query=mysql_query($sql);
041$rows=mysql_num_rows($query);
042if($rows>0){
043echo"<scripttype='text/javascript'>alert('用戶名已存在');location='javascript:history.back()';</script>";
044}else{
045$user_in="insertintouser_info(username,pass,sex,qq,email,img)values('$_POST[username]',md5('$_POST[pass]'),'$_POST[sex]','$_POST[qq]','$_POST[email]','$_POST[img_select]')";
046//echo$user_in;
047mysql_query($user_in);
048echo"<scripttype='text/javascript'>alert('寫入成功!!');location.href='login.php';</script>";
049
050}
051
052//javascript:history.go(-1)
053
054}
055?>
056
057
058<formaction="reg.php"name="reg_form"method="post"onsubmit="returncheck_reg()">
059<tablename="reg_table"align="left">
060<tr>
061<td>用戶:</td><td><inputid="username"name="username"class="myText"type="text"maxlength="12"/></td>
062</tr>
063
064<tr><!--性別:0保密1女2男-->
065<td>性別:</td>
066<td>女<inputtype="radio"value="1"name="sex"/>
067男<inputtype="radio"value="2"name="sex"/>
068保密<inputtype="radio"value="0"name="sex"checked/></td>
069</tr>
070
071<tr>
072<td>密碼:</td><td><inputname="pass"class="myText"type="password"onblur="check_len(this)"/><spanid="show_pass"style="color:red;"></span></td>
073</tr>
074
075<tr>
076<td>重復密碼:</td><td><inputname="repass"class="myText"type="password"onblur="check_pass(this)"/><spanid="show_repass"style="color:red;"></span></td>
077</tr>
078
079<tr>
080<td>QQ:</td><td><inputtype="text"class="myText"name="qq"onblur="check_qq(this)"/><spanstyle="color:red;"id="show_qq"></span></td>
081</tr>
082
083<tr>
084<td>郵箱:</td><td><inputtype="text"class="myText"name="email"onblur="check_email(this)"/><spanid="show_e"style="color:red;"></span></td>
085</tr>
086
087<tr>
088<tdheight="60">頭像:</td>
089<td>
090<selectname="img_select"onchange="img_change(this)">
091<optionvalue="101">女001</option>
092<optionvalue="102">女002</option>
093<optionvalue="103">女003</option>
094<optionvalue="104">女004</option>
095<optionvalue="105">男001</option>
096<optionvalue="106">男002</option>
097<optionvalue="107">男003</option>
098<optionvalue="108">男004</option>
099</select>
100<imgsrc="/bbs/img/101.gif"id="tx_change"style="width:50px;height:65px;"alt=""/>
101</td>
102</tr>
103
104<trheight="20"align="justify">
105<tdalign="right"><inputtype="submit"value="注冊"name="submit"style="margin-right:5px;"/></td>
106<td><inputtype="reset"value="重置"name="reset"style="margin-left:5px;"/></td>
107</tr>
108
109<tr>
110<tdcolspan="2">我已有賬號現在<ahref="login.php">登錄</a></td>
111</tr>
112
113</table>
114</form>
115</body>
116</html>
fun.js的代碼如下:

01//根據下拉框變換圖片
02functionimg_change(thisObj){
03varimgsrc="/bbs/img/"+thisObj.value+".gif";
04document.getElementById("tx_change").src=imgsrc;
05}
06
07//檢查是否都符合注冊要求
08functioncheck_reg()
09{
10if(check_len()&&check_pass()&&check_email()&&check_qq())
11{
12returntrue;
13}else{
14returnfalse;
15}
16}
17
18//檢查密碼長度不能少於6
19functioncheck_len(thisObj){
20if(thisObj.value.length==0)
21{
22document.getElementById('show_pass').innerHTML="密碼不能為空";
23returnfalse;
24}else{
25if(thisObj.value.length<6)
26{
27document.getElementById('show_pass').innerHTML="密碼長度不少於6";
28returnfalse;
29}
30document.getElementById('show_pass').innerHTML="";
31returntrue;
32}
33}
34
35//檢查倆次密碼輸入是否一致
36functioncheck_pass(thisObj){
37varpsw=document.getElementById('pass');
38if(psw.value.length==0)
39{
40document.getElementById('show_pass').innerHTML="密碼不能為空";
41returnfalse;
42}else{
43document.getElementById('show_pass').innerHTML="";
44
45if(thisObj.value!=psw.value)
46{
47document.getElementById('show_repass').innerHTML="兩次密碼輸入不正確";
48returnfalse;
49}
50document.getElementById('show_repass').innerHTML="";
51returntrue;
52}
53}
54
55//檢查email是否正確
56functioncheck_email(thisObj){
57varreg=/^([a-zA-Zd][a-zA-Z0-9_]+@[a-zA-Zd]+(.[a-zA-Zd]+)+)$/gi;
58varrzt=thisObj.value.match(reg);
59if(thisObj.value.length==0){
60document.getElementById('show_e').innerHTML="Email不能為空";
61returnfalse;
62}else{
63if(rzt==null)
64{
65document.getElementById('show_e').innerHTML="Email地址不正確";
66returnfalse;
67}
68document.getElementById('show_e').innerHTML="";
69returntrue;
70}
71
72}
73
74//檢查qq格式是否正確
75functioncheck_qq(thisObj){
76varqq=document.getElementById('qq').value;
77varreg=/^d+$/;
78if(qq.search(reg))
79{
80document.getElementById('show_qq').innerHTML="QQ只能為數字";
81returnfalse;
82}else{
83document.getElementById('show_qq').innerHTML="";
84returntrue;
85}
86
87
88}

5. 在PHP中判斷注冊用戶名是否存在如何做

php中 判斷資料庫
一般是 select `uid` form `member` where `username` = $_POST['username'] limit 1
這只是一段SQL
裡面的安全和實現我想你應該明白
我只是和你講原因
當你獲取注冊的用戶名,並where 與資料庫里查詢,如果匹配的那會返回那段 username 對應的uid 編號 或者返回其它也可以
如果判斷沒有 則就返回是NULL 空
那麼你就可以這樣判斷
if(!empty($row_member['uid'])){
//用戶名已經被注冊
}else{
//用戶名可以使用
}

6. php中判斷用戶名是否已經存在始終無法成功,後面的注釋掉列印var_mp("mysqli_num_rows($res)")也不行

兩個建議:
一、set name在select查詢語句之前
二、不要使用mysqli_num_row判斷,而是直接fetch結果到數組$row,看fetch是否成功,然後看$row['username']==$username來確認。

7. php判斷用戶名是否存在

if($res && mysql_num_rows($res)>0){

這一行,改成
if(mysql_num_rows($res)>0){
試一下

8. 關於Jquery+ajax驗證用戶名在資料庫是否存在的問題

myVlidate_jquery.js文件代碼如下:
//驗證用戶是否存在
function validate() {

// 1獲取文本框的內容
// document.getElementById("userName"); dom的方式
// Jquery的查找節點的方式,參數中#加上id屬性值可以找到一個節點
var obj = $("#userName");
var userName = obj.val();
//alert("userName: " + userName);

// 2將獲取到的內容發送給伺服器的servlet
// 調用jquery的XMLHTTPrequest對象get請求的封裝
//$.get("servlet/CheckUserName?userName=" + userName, null, callback);
//JS中定義一個簡單對象的格式
var jsObj={name:"123",age:30};
//使用jquery的XMLHTTPRequest對象get請求的封裝
$.ajax({
type:"POST", //http請求方式
url:"servlet/AjaxXMLServer", //發送給伺服器的url
data:"userName="+userName, //發送給伺服器的參數
dataType:"xml", //告訴JQUERY返回的數據格式(注意此處數據格式一定要與提交的servlet返回的數據格式一致,不然不會調用callback)
success:callback //定義交互完成,並且伺服器正確返回數據時調用回調函數
});
}

9. php ajax驗證用戶名是否存在

$("button").click(function(){
$.get("ajax_login.php",{username:'testname'},function(result){
if(result){//判斷已經存在
alert('用戶名已經存在');
}else{
alert('可以注冊');
}
});
});


給你一個js端,可以結合樓上勰莫莫的服務端一起用

10. PHP 驗證用戶名是否存在

是因為你的sql語句寫錯了,你寫的是 $sql="select * from userlist where 'user'='$user'";
這個是錯誤的,user 兩邊不是引號,是數字1旁邊的`,改成`user`='$user' 這樣就對了,或者user='$user'也行。

閱讀全文

與phpjquery驗證用戶名是否存在相關的資料

熱點內容
php獨立運行 瀏覽:530
手機sh執行命令 瀏覽:727
雲伺服器的角色 瀏覽:733
單片機頻率比例 瀏覽:840
我的世界伺服器如何關閉正版驗證 瀏覽:504
如何查roid伺服器上的 瀏覽:130
安卓手機主板如何撬晶元不掉電 瀏覽:249
php各個框架的優缺點 瀏覽:101
php1100生成數組 瀏覽:359
以後做平面設計好還是程序員好 瀏覽:552
雲伺服器應用管理 瀏覽:438
飢荒雲伺服器搭建過程 瀏覽:186
可編程式控制制器優點 瀏覽:99
壓縮垃圾車說明書 瀏覽:28
五輪書pdf 瀏覽:802
單片機定時流水中斷系統流水燈 瀏覽:701
u8如何連接伺服器配置 瀏覽:66
動力在於緩解壓力 瀏覽:867
報考科一用什麼app 瀏覽:346
knn人臉識別演算法 瀏覽:431