จากบทความ ตัวอย่างฟอร์มการกรอกข้อมูลส่วนตัวโดยใช้ Javascript ผมได้เขียนถึงการกรอกข้อมูลในแบบฟอร์มโดยใช้โค้ด php + javascript มาบทความนี้เลยขอกล่าวถึงการใช้เฉพาะโค้ด php อย่างเดียวบ้าง ซึ่งมันก็ดูดีไปอีกแบบ ลักษณะการทำงานก็เหมือนเดิมคือเวลากรอกข้อมูลผิด หรือกรอกไม่ครบถ้วนก็จะมีข้อความเตือนขึ้นมา มาดูตัวอย่างกันครับ
ตัวอย่างโค้ด simple2.php
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Sample 2</title>
<style>
body { padding: 10px 0px 0px 0px; }
td
{
padding: 2px 5px;
text-align: left;
}
h4 { margin: 0px; }
input.editfield, select.editfield, textarea.editfield { width: 18em; }
</style>
</head>
<body>
<center>
<form method="post" action="sample2.php">
<input type="hidden" name="form_validation_rules" value="" />
<input type="hidden" name="form_validation_behaviours" value="
Details : count details_count 20 black red;
Year : next 4;
Month Day : next 2;
Month Day : prev;
" />
<table>
<?php
if (isset($_POST['submit']))
{
include 'form-validation.php';
$result = form_validation_validate($_POST, "
Username Password Year Month Day Details : empty;
Username Password : len >= 3;
Username Password : chnum_;
E-mail: email;
Membership : select 0;
Year : > 1900;
Year Month Day : date;
Sex : radio;
Details: len <= 20;
Categories[] Newsletters[] : cnt >= 2;
Agree : terms;
");
?>
<tr>
<td colspan="2">
<?php
if ($result === true)
{
echo '<span style="color: green;">The form is successfully submitted!</span>';
$_POST = Array();
}
else echo '<span style="color: red;">' . $result . '</span>';
?>
</td>
</tr>
<?php
}
function __post($name, $val = '')
{
return isset($_POST[$name]) ? $_POST[$name] : $val;
}
function __checked($name, $val)
{
return isset($_POST[$name]) && ($_POST[$name] == $val) ? 'checked="checked"' : '';
}
function __checked2($name, $ind)
{
return isset($_POST[$name]) && in_array($ind, $_POST[$name]) ? 'checked="checked"' : '';
}
function __selected($name, $val)
{
return isset($_POST[$name]) && ($_POST[$name] == $val) ? 'selected="selected"' : '';
}
function __selected2($name, $ind)
{
return isset($_POST[$name]) && in_array($ind, $_POST[$name]) ? 'selected="selected"' : '';
}
?>
<tr>
<td style="padding-bottom: 10px;" colspan="2"><h4>Sample 2</h4></td>
</tr>
<tr>
<td>Username:</td>
<td><input class="editfield" type="text" name="Username" value="<?=__post('Username');?>" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input class="editfield" type="password" name="Password" value="<?=__post('Password');?>" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input class="editfield" type="text" name="E-mail" value="<?=__post('E-mail');?>" /></td>
</tr>
<tr>
<td>Membership:</td>
<td>
<select class="editfield" name="Membership">
<option value="0" <?=__selected('Membership', 0);?>></option>
<option value="1" <?=__selected('Membership', 1);?>>Silver</option>
<option value="2" <?=__selected('Membership', 2);?>>Gold</option>
<option value="3" <?=__selected('Membership', 3);?>>Platinum</option>
</select>
</td>
</tr>
<tr>
<td>Age:</td>
<td>
<input type="text" name="Year" value="<?=__post('Year'); ?>" size="4" maxlength="4" />
<input type="text" name="Month" value="<?=__post('Month');?>" size="2" maxlength="2" />
<input type="text" name="Day" value="<?=__post('Day'); ?>" size="2" maxlength="2" />
<small>yyyy/mm/dd</small>
</td>
</tr>
<tr>
<td>Sex:</td>
<td>
<input type="radio" name="Sex" value="male" <?=__checked('Sex', 'male' );?> /> Male
<input type="radio" name="Sex" value="female" <?=__checked('Sex', 'female');?> /> Female
</td>
</tr>
<tr>
<td>Details:</td>
<td>
<textarea class="editfield" name="Details" rows="4"><?=__post('Details');?></textarea>
<br />
<small><span id="details_count">0</span> out of 20 characters</small>
</td>
</tr>
<tr>
<td>Categories:</td>
<td>
<select class="editfield" name="Categories[]" multiple="multiple" size="4">
<option value="0" <?=__selected2('Categories', 0);?>>Cars</option>
<option value="1" <?=__selected2('Categories', 1);?>>Computers</option>
<option value="2" <?=__selected2('Categories', 2);?>>Dogs</option>
<option value="3" <?=__selected2('Categories', 3);?>>Food</option>
</select>
<br />
<small>Use ctrl+mouse to select multiple categories</small>
</td>
</tr>
<tr>
<td>Newsletters:</td>
<td>
<input type="checkbox" name="Newsletters[]" value="0" <?=__checked2('Newsletters', 0);?> /> Cars <br />
<input type="checkbox" name="Newsletters[]" value="1" <?=__checked2('Newsletters', 1);?> /> Computers <br />
<input type="checkbox" name="Newsletters[]" value="2" <?=__checked2('Newsletters', 2);?> /> Dogs <br />
<input type="checkbox" name="Newsletters[]" value="3" <?=__checked2('Newsletters', 3);?> /> Food
</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" name="Agree" value="Yes" <?=__checked('Agree', 'Yes');?> />
<small>By checking this box you agree to some terms</small>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
<script src="form-validation.js">
</script>
</center>
</body>
</html>
ส่วนอันนี้เป็นไฟล์ประกอบน่ะครับ form-validation.js และ form-validation.php download
ไม่มีความคิดเห็น:
แสดงความคิดเห็น