|
|
|
แจกโค้ด เช็กฟอร์มครับ (validate form) เอาไปต่อยอดกันเองนะครับ |
|
|
|
|
|
|
|
แจกโค้ด เช็กฟอร์มครับ เอาไปต่อยอดเอาเองนะครับ ใช้ดีแล้วช่วยบอกต่อหน่อยนะครับ อิอิ
--------------- index.html ----------------------
<html>
<head>
<title>Validate Form</title>
</head>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
}
function getresult() {
doCallAjax();
var url = 'validate.php';
var pmeters = 'myName='+encodeURI( document.getElementById("txtName").value )+
'&myMail='+encodeURI( document.getElementById("txtMail").value )+
'&myOld='+encodeURI( document.getElementById("txtOld").value );
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
var explode = HttPRequest.responseText.split("\n"); // แยก
var shown = false;
for ( var i in explode )
{
var explode_again = explode[i].split("|");
if (explode_again[0]=='error')
{
shown = true;
document.getElementById('err_'+explode_again[1]).innerHTML = explode_again[2];
document.getElementById('err_'+explode_again[1]).style.display = "";
}else if (explode_again[0]=='ok') {
document.getElementById('err_'+explode_again[1]).style.display = "none";
}
}
if ( ! shown )
{
document.getElementById('myForm').submit();
}
}
}
}
</script>
<body>
<form action="addcomment.php" method="post" onsubmit="return false; " id="myForm">
Name:<input type="text" name="txtName" id="txtName" value="" ><span id="err_myName"></span><br>
E-mail<input type="text" name="txtMail" id="txtMail" value=""><span id="err_myMail"></span><br>
Old<input type="text" name="txtOld" id="txtOld" value=""><span id="err_myOld"></span><br>
<input name="btnButton" id="btnButton" type="submit" value="Submit" onClick="getresult();">
</form>
</body>
</html>
--------------- validate.php ----------------------
<?php
$errors = array ();
$errors_msg = null;
if ($_POST["myName"] == ""){
$errors[ 'myName' ] [] = "ไม่ได้กรอกชื่อ";
}else if($_POST["myName"] == "boy"){
$errors['myName'] [] = "ชื่อนี้มีคนใช้แล้วจ้า";
}
if ($_POST["myMail"] == ""){
$errors[ 'myMail' ] [] = "ไม่ได้กรอกเมล";
}else {
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST["myMail"])){
$errors[ 'myMail' ] [] = "เมลไม่ถูกต้อง";
}
}
if ($_POST["myOld"] == ""){
$errors[ 'myOld' ] [] = "ไม่ได้กรอกอายุ";
}
if ( is_array ( $_POST ) )
{
foreach ( $_POST as $key => $value )
{
if ( array_key_exists ( $key, $errors ) )
{
foreach ( $errors [ $key ] as $k => $v )
{
$errors_msg .= "error|$key|$v\n";
}
}
else {
$errors_msg .= "ok|$key\n";
}
}
}
echo $errors_msg;
?>
Tag : PHP
|
ประวัติการแก้ไข 2011-02-04 20:40:21 2011-02-04 21:20:18 2011-02-05 12:32:10
|
|
|
|
|
Date :
2011-02-04 20:34:11 |
By :
looktevada |
View :
1815 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอเอาไปลอง ก่อนน่ะครับ thk
|
|
|
|
|
Date :
2011-02-04 21:00:28 |
By :
rakmomteesud |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2011-02-05 12:37:17 |
By :
looktevada |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2011-05-13 14:49:56 |
By :
พี |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดีมะ
|
|
|
|
|
Date :
2011-05-14 09:53:29 |
By :
กก |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|