|
|
|
ขอความช่วยเหลือหน่อยครับ เรื่อง PHP + Ajax เกี่ยวกับ Form Register |
|
|
|
|
|
|
|
คือว่าผมได้เขียน Form Register โดยใช้ PHP กับ ajax อะครับแต่คือเกิดปัญหาตรงที่ ตรงสอบว่าผู้ใช้ใส่อะไรได้ แต่คือ ถ้าผู้ใช้ใส่ผิดจะทำไม่ให้ submit ผ่านยังไงอะครับ
Code
<script language="javascript">
function tickbox(tb){
if(tb.checked==true){
document.regisform.cfsubmit.disabled=false;
}else{
document.regisform.cfsubmit.disabled=true;
}
}
function createobj(){
var obj=false;
if(window.XMLHttpRequest){
obj=new XMLHttpRequest;
}else if(window.ActiveXObject){
obj=new ActiveXObject("Microsoft.XMLHTTP");
}else{
alert("Your Browser does not support");
}return obj;
}var obj=createobj();
function checkregis(me,type,tvalue,tvalue2,tvalue3){
if(obj){
obj.onreadystatechange=function(){
if(obj.readyState==4&&obj.status==200){
var urldata=document.getElementById(me);
urldata.innerHTML=obj.responseText;
}
}
var data="checkregis.php";
data=data+"?type="+type+"&tvalue="+tvalue+"&tvalue2="+tvalue2+"&tvalue3="+tvalue3;
obj.open("GET",data,true);
}obj.send(null);
}
function checknull(){
if(document.regisform.email.value==""){
document.regisform.email.focus();
alert("กรุณาใส่ \"อีเมลของคุณ\" ด้วยค่ะ");
return false;
}else if(document.regisform.pass01.value==""){
document.regisform.pass01.focus();
alert("กรุณาใส่ \"รหัสผ่าน\" ด้วยค่ะ");
return false;
}else if(document.regisform.pass02.value==""){
document.regisform.pass02.focus();
alert("กรุณาใส่ \"รหัสผ่านอีกครั้ง\" ด้วยค่ะ");
return false;
}else if(document.regisform.firstname.value==""){
document.regisform.firstname.focus();
alert("กรุณาใส่ \"ชื่อจริง\" ด้วยค่ะ");
return false;
}else if(document.regisform.lastname.value==""){
document.regisform.lastname.focus();
alert("กรุณาใส่ \"นามสกุล\" ด้วยค่ะ");
return false;
}else if(document.regisform.born.value==""){
document.regisform.born.focus();
alert("กรุณาใส่ \"วันเกิด\" ด้วยค่ะ");
return false;
}else{
return true;
}
}
</script>
<meta charset="windows-874">
<form name="regisform" method="post" action="regis.php" onsubmit="return checknull()">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr><td align="center">
<table width="100%" border="0">
<tr>
<td align="right" width="40%" valign="top">อีเมลของคุณ :</td>
<td width="60%">
<input type="text" name="email" maxlength="60" onblur="checkregis('msgerror1','email',this.value)">
<font color="red" size="2"><div id="msgerror1"></div></font>
<font size="2" color="gray">เช่น [email protected] ชื่อนี้จะใช้เพื่อเข้าสู่ระบบ</font>
</td>
</tr>
<tr>
<td align="right" valign="top">สร้างรหัสผ่าน :</td>
<td>
<input type="text" name="pass01" maxlength="12" onblur="checkregis('msgerror2','pass01',this.value)">
<font color="red" size="2"><div id="msgerror2"></div></font>
<font size="2" color="gray">ความยาวอย่างน้อย 6 ตัวอักษร</font>
</td>
</tr>
<tr>
<td align="right" valign="top">ยืนยันรหัสผ่าน :</td>
<td>
<input type="text" name="pass02" maxlength="12" onblur="checkregis('msgerror3','pass02',this.value,document.regisform.pass01.value)">
<font color="red" size="2"><div id="msgerror3"></div></font>
</td>
</tr>
</table>
</td></tr>
<tr><td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td align="center"><font size="5" color="gray">- - - - - - - - - - - - - - - - - - - - - - - - - - -</font></td></tr>
</table>
</td></tr>
<tr><td align="center">
<table width="100%" border="0">
<tr>
<td align="right" width="40%" valign="top">ชื่อจริง :</td>
<td width="60%">
<input type="text" name="firstname" maxlength="20" onblur="checkregis('msgerror4','firstname',this.value)">
<font color="red" size="2"><div id="msgerror4"></div></font>
</td>
</tr>
<tr>
<td align="right" valign="top">นามสกุล :</td>
<td>
<input type="text" name="lastname" maxlength="30" onblur="checkregis('msgerror5','lastname',this.value)">
<font color="red" size="2"><div id="msgerror5"></div></font>
</td>
</tr>
<tr>
<td align="right">เพศ :</td>
<td>
<input type="radio" name="gender" value="1" checked>ชาย
<input type="radio" name="gender" value="0">หญิง
</td>
</tr>
<tr>
<td align="right" valign="top" valign="top">วันเกิด : </td>
<td>
<select name="month" style="text-align:center">
<option value="0">เดือน
<option value="1">มกราคม
<option value="2">กุมภาพันธ์
<option value="3">มีนาคม
<option value="4">เมษายน
<option value="5">พฤษภาคม
<option value="6">มิถุนายน
<option value="7">กรกฎาคม
<option value="8">สิงหาคม
<option value="9">กันยายน
<option value="10">ตุลาคม
<option value="11">พฤศจิกายน
<option value="12">ธันวาคม
</select>
<input type="text" name="day" maxlength="2" style="width:25;" onblur="checkregis('msgerror6','day',this.value)">
<input type="text" name="year" maxlength="4" style="width:35;" onblur="checkregis('msgerror6','year',this.value,document.regisform.month.value,document.regisform.day.value)">
<font color="red" size="2"><div id="msgerror6"></div></font>
<font size="2" color="gray">เช่น "มกราคม/9/2011"</font>
</td>
</tr>
</table>
</td></tr>
<tr><td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td align="center"><font size="5" color="gray">- - - - - - - - - - - - - - - - - - - - - - - - - - -</font></td></tr>
</table>
</td></tr>
<tr><td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td align="center">
<input type="checkbox" name="cfregis" onclick="tickbox(this)"><font size="2">ข้าพเจ้ายอมรับข้อตกลงการใช้งานทั้งหมดของ Relax (คลิกอ่านรายละเอียด)</font>
</td></tr>
<tr><td align="center">
<input type="submit" value="สร้างบัญชี" name="cfsubmit" style="width:120;" disabled> <input type="reset" value="ล้างข้อมูล" style="width:120">
</td></tr>
</table>
</td></tr>
</table>
</form>
Tag : PHP, HTML/CSS, JavaScript, Ajax
|
|
|
|
|
|
Date :
2011-12-18 17:23:26 |
By :
CrazyLine |
View :
1508 |
Reply :
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ส่วนของ ตัวเช็ค อันนี้ครับ
Code
<?php
header("content-type: text/html; charset=tis-620");
$type=$_GET["type"];
$tvalue=$_GET["tvalue"];
$tvalue2=$_GET["tvalue2"];
$tvalue3=$_GET["tvalue3"];
if($type=="email"){
$mail_pattern="/^[[:alnum:]._-]+@[[:alnum:]]+\.[[:alnum:]]+$/";
if(!preg_match($mail_pattern,$tvalue)){
echo "<font color='red' size=2>โปรดตรวจสอบ \"อีเมลของคุณ\" ด้วยค่ะ</font>";
}
}else if($type=="pass01"){
if($tvalue==""){
echo "<font color='red' size=2>โปรด \"สร้างรหัสผ่าน\" ด้วยค่ะ</font>";
}else if(strlen($tvalue)<6){
echo "<font color='red' size=2>โปรด \"สร้างรหัสผ่าน\" มากกว่า 6 ตัวอักษร</font>";
}else if(!preg_match("/^[a-zA-Z0-9]+$/",$tvalue)){
echo "<font color='red' size=2>ห้ามใส่อักขระพิเศษและเป็นภาษาอังกฤษหรือตัวเลขเท่านั้นค่ะ</font>";
}else{
echo "<font color='green' size=2>สามารถใช้รหัสผ่านนี้ได้ค่ะ</font>";
}
}else if($type=="pass02"){
if($tvalue==""){
echo "<font color='red' size=2>โปรด \"ยืนยันรหัสผ่าน\" ด้วยค่ะ</font>";
}else if(strlen($tvalue)<6){
echo "<font color='red' size=2>โปรด \"ยืนยันรหัสผ่าน\" มากกว่า 6 ตัวอักษร</font>";
}else if(!preg_match("/^[a-zA-Z0-9]+$/",$tvalue)){
echo "<font color='red' size=2>ห้ามใส่อักขระพิเศษค่ะ</font>";
}else if($tvalue!=$tvalue2){
echo "<font color='red' size=2>\"ยืนยันรหัสผ่าน\" ไม่ตรงกับ \"สร้างรหัสผ่าน\" ค่ะ</font>";
}
}else if($type=="firstname"){
if($tvalue==""){
echo "<font color='red' size=2>โปรดใส่ \"ชื่อจริง\" ด้วยค่ะ</font>";
}else if(!preg_match("/^[a-zA-Zก-ฮ]+$/",$tvalue)){
echo "<font color='red' size=2>ห้ามใส่อักขระพิเศษหรือตัวเลขค่ะ</font>";
}
}else if($type=="lastname"){
if($tvalue==""){
echo "<font color='red' size=2>โปรดใส่ \"นามสกุล\" ด้วยค่ะ</font>";
}else if(!preg_match("/^[a-zA-Zก-ฮ]+$/",$tvalue)){
echo "<font color='red' size=2>ห้ามใส่อักขระพิเศษหรือตัวเลขค่ะ</font>";
}
}else if($type=="day"){
if($tvalue==""){
echo "<font color='red' size=2>โปรดใส่ \"วันเกิด\" ด้วยค่ะ</font>";
}else if($tvalue>31||$tvalue<1){
echo "<font color='red' size=2>โปรดใส่ \"วันเกิด\" ให้ถูกต้องค่ะ</font>";
}
}else if($type=="year"){
$modyear=$tvalue%4;
if($modyear==0){
switch($tvalue2){
case 1: $maxday=31;
case 2: $maxday=29;
case 3: $maxday=31;
case 4: $maxday=30;
case 5: $maxday=31;
case 6: $maxday=30;
case 7: $maxday=31;
case 8: $maxday=31;
case 9: $maxday=30;
case 10: $maxday=31;
case 11: $maxday=30;
case 12: $maxday=31;
}
if($tvalue3>$maxday||$tvalue3<1){
echo "<font color='red' size=2>โปรดใส่ \"วันเกิด\" ให้ถูกต้องค่ะ</font>";
}
}else{
switch($tvalue2){
case 1: $maxday=31;
case 2: $maxday=28;
case 3: $maxday=31;
case 4: $maxday=30;
case 5: $maxday=31;
case 6: $maxday=30;
case 7: $maxday=31;
case 8: $maxday=31;
case 9: $maxday=30;
case 10: $maxday=31;
case 11: $maxday=30;
case 12: $maxday=31;
}
if($tvalue3>$maxday&&$tvalue<1){
echo "<font color='red' size=2>โปรดใส่ \"วันเกิด\" ให้ถูกต้องค่ะ</font>";
}
}
}else{
return true;
}
?>
|
|
|
|
|
Date :
2011-12-18 17:24:42 |
By :
CrazyLine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<input type="submit" value="สร้างบัญชี" name="cfsubmit" style="width:120;" disabled onClick="return checknull();">
checknull คือฟังก์ัั่ชั่นตรวจสอบค่าว่างครับ ไม่แน่ใจว่าคุณใช้ฟังก์ชั่นนี้หรือป่าว
|
|
|
|
|
Date :
2011-12-18 19:10:57 |
By :
000su000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ในหน้าสองละกัน เห็นบอกว่าเป็นเช็คนะ
ในหน้าเช็คก็ให้เอาค่ารหัสที่ถูกส่งมาไปเช็คครับ ก็คือเอา ชื่อ กับ พาส ไปทำการ query ข้อมูลพอ fetch_array แล้วก็ใช้ if เช็คว่า
ชื่อ และ พาสนั้นถูกต้องป่าว ถ้าไม่ถูกก็อาจจะ alert ข้อความบอกว่าผิด แล้วก็ให้กับไปกรอกใหม่น่ะ แบบนี้น่ะ
$dbarr=mysql_fetch_array($query);
if($dbarr['user']=='' and $dbarr['pass']==''){
echo "<script language='javascript'>alert('ไม่สามารถล็อคอินได้ กับไปใส่ไหม่นะจ๊ะ'); history.back();</script>";
exit; //เพื่อกันไม่ให้ทำรายการต่อไป ถ้าาข้อมูลผิดพลาด
}
|
|
|
|
|
Date :
2011-12-18 19:28:01 |
By :
ozma |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คือผมจะบอกไงดีอะ คนละเรื่องเดียวกันเลยอะครับ
คืองี้ code ที่ผมเขียนอะ มันจะมีบัคอย่างนึง
สมมุตเราใส่ไปแล้ว เช็คได้นะครับว่างหรืออะไร แล้วก็เช็คความถูกต้อง patform จาก ajax ได้ด้วย
แต่คือ สมมุตว่า ผมกรอกผิดหมดเลย ajax ก็คืนค่ามาว่า ผิด แต่ก็ยัง submit ไปต่อได้เหมือนเดิม
อันนี้อะครับคือปัญหา
|
|
|
|
|
Date :
2011-12-19 16:43:24 |
By :
CrazyLine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันนี้อะครับ กด สร้าง มันก็ไปต่อทันที
|
|
|
|
|
Date :
2011-12-19 16:47:00 |
By :
CrazyLine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดู ผมก็งูๆ ปลาๆ เหมือนกันน่ะ
อันนี้มันจะแสดงเป็น alert ข้อความบอกนะ
Code (PHP)
<form name="regisform" method="post" action="regis.php" onsubmit="return checknull()">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr><td align="center">
<table width="100%" border="0">
<tr>
<td align="right" width="40%" valign="top">อีเมลของคุณ :</td>
<td width="60%">
<input type="text" name="email">
<font color="red" size="2"><div id="msgerror1"></div></font>
<font size="2" color="gray">เช่น [email protected] ชื่อนี้จะใช้เพื่อเข้าสู่ระบบ</font>
</td>
</tr>
<tr>
<td align="right" valign="top">สร้างรหัสผ่าน :</td>
<td>
<input type="text" name="pass01">
<font color="red" size="2"><div id="msgerror2"></div></font>
<font size="2" color="gray">ความยาวอย่างน้อย 6 ตัวอักษร</font>
</td>
</tr>
<tr>
<td align="right" valign="top">ยืนยันรหัสผ่าน :</td>
<td>
<input type="text" name="pass02">
<font color="red" size="2"><div id="msgerror3"></div></font>
</td>
</tr>
</table>
</td></tr>
<tr><td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td align="center"><font size="5" color="gray">- - - - - - - - - - - - - - - - - - - - - - - - - - -</font></td></tr>
</table>
</td></tr>
<tr><td align="center">
<table width="100%" border="0">
<tr>
<td align="right" width="40%" valign="top">ชื่อจริง :</td>
<td width="60%">
<input type="text" name="firstname">
<font color="red" size="2"><div id="msgerror4"></div></font>
</td>
</tr>
<tr>
<td align="right" valign="top">นามสกุล :</td>
<td>
<input type="text" name="lastname">
<font color="red" size="2"><div id="msgerror5"></div></font>
</td>
</tr>
<tr>
<td align="right">เพศ :</td>
<td>
<input type="radio" name="gender" value="1">ชาย
<input type="radio" name="gender" value="0">หญิง
</td>
</tr>
<tr>
<td align="right" valign="top" valign="top">วันเกิด : </td>
<td>
<select name="month">
<option value="">เดือน</option>
<option value="1">มกราคม</option>
<option value="2">กุมภาพันธ์</option>
<option value="3">มีนาคม</option>
<option value="4">เมษายน</option>
<option value="5">พฤษภาคม</option>
<option value="6">มิถุนายน</option>
<option value="7">กรกฎาคม</option>
<option value="8">สิงหาคม</option>
<option value="9">กันยายน</option>
<option value="10">ตุลาคม</option>
<option value="11">พฤศจิกายน</option>
<option value="12">ธันวาคม</option>
</select>
<input type="text" name="day">
<input type="text" name="year">
<font color="red" size="2"><div id="msgerror6"></div></font>
<font size="2" color="gray">เช่น "มกราคม/9/2011"</font>
</td>
</tr>
</table>
</tr>
<tr><td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td align="center"><font size="5" color="gray">- - - - - - - - - - - - - - - - - - - - - - - - - - -</font></td></tr>
</table>
</td></tr>
<tr><td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td align="center">
<input type="checkbox" name="cfregis" onclick="tickbox(this)"><font size="2">ข้าพเจ้ายอมรับข้อตกลงการใช้งานทั้งหมดของ Relax (คลิกอ่านรายละเอียด)</font>
</td></tr>
<tr><td align="center">
<input type="submit" value="สร้างบัญชี"/>
<input type="reset" value="ล้างข้อมูล">
</td></tr>
</table>
</td></tr>
</table>
<script language="javascript">
function checknull(){
if(document.regisform.email.value==""){
alert('กรุณาใส่อีเมลของคุณด้วยค่ะ');
return false;
}else if(document.regisform.pass01.value=="" & document.regisform.pass01.value.lenght<5){
alert('กรุณาใส่รหัสผ่านด้วยค่ะ');
return false;
}else if(document.regisform.pass02.value=="" & document.regisform.pass01.value.lenght<5){
alert('กรุณาใส่รหัสผ่านอีกครั้งด้วยค่ะ');
return false;
} else if(document.regisform.pass01.value != document.regisform.pass02.value){
alert('รหัสไม่ตรงกัน');
return false;
} else if(document.regisform.firstname.value==""){
alert('กรุณาใส่ชื่อจริงด้วยค่ะ');
return false;
} else if(document.regisform.lastname.value==""){
alert('กรุณาใส่นามสกุลด้วยค่ะ');
return false;
} else if(document.regisform.month.value=="" || document.regisform.day.value=="" || document.regisform.year.value==""){
alert('กรุณาใส่วันเกิดด้วยค่ะ');
return false;
} else if(isNaN(document.regisform.day.value)){
alert('1111');
return false;
} else if(isNaN(document.regisform.year.value)){
alert('222222');
return false;
} else if(document.regisform.cfregis.checked==""){
alert('00000');
return false;
} else {
return true;
}
}
</script>
</form>
|
|
|
|
|
Date :
2011-12-19 18:54:32 |
By :
ozma |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คือผมจะบอกว่าไงดีอะ แบบนั้น ผมได้แล้ว T-T ที่ไม่ได้คือจะรับ ture false จาก ajax ยังไง
|
|
|
|
|
Date :
2011-12-19 19:51:54 |
By :
CrazyLine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
สงสัยจะไม่มีใครเข้าใจผมเลย งั้นตัวอย่าง อย่าง google ครับ เขาไม่สามารถ submit ได้ถ้าใส่มั่ว แต่ของผมผ่านตลอดไม่มีเบรก
|
|
|
|
|
Date :
2011-12-19 22:08:57 |
By :
CrazyLine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าทำมาขนาดนี้แล้ว ไม่อยากโล๊ะทิ้งก็เอาวิธีนี้ละกันนะครับ
1. สร้าง hidden field ขึ้นมาหนึ่งตัว ตั้งชื่อว่า has_error หรือว่าอะไรก็แ้ล้วแต่จะกรุณา ใส่ค่าให้มันเป็น 0
2. checkregis ฟังก์ชั่นนี้ ถ้าเกิด error ยัดค่าให้ has_error มัน เป็น 1 ซะ
3. checknull ให้เพิ่ม if เช็ค has_error ว่าเป็น 1 หรือเปล่า ถ้าเป็น 1 return false ถ้าเป็น 0 return true
จบกระบวนการกู้โลก ณ บัดนาว
/me เผ่น
|
ประวัติการแก้ไข 2011-12-19 23:10:47
|
|
|
|
Date :
2011-12-19 23:10:20 |
By :
ikikkok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับเดียวลองดู
|
|
|
|
|
Date :
2011-12-20 13:42:52 |
By :
CrazyLine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เอาโค๊ดที่คุณบอกว่าทำไม่ได้มาดูครับ
|
|
|
|
|
Date :
2011-12-21 00:51:38 |
By :
ikikkok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|