PHP
·
发表于 5年以前
·
阅读量:8297
±¾ÎÄʵÀý½²ÊoÁËCI¿o¼Ü±iµ¥ÑeÖ¤µÄ½¨¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
1¡¢formÍ*²¿ÐÅÏ¢µÄ×Ô¶¯Êa³oº¯Êý(view)
<?php
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);
//ÉÏÃaeÒ»ÐдuÂeÊa³o£º
//<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" id="myform" class="email"/>
/*
* form_open_multipart()
* º¯ÊýÓÃ*¨Í¬ÉÏ£¬¼ÓÉÏÁËÎļþÉÏ´«µÄÐÅÏ¢ ÉÏ´«*½Ê½Ä¬ÈÏΪpost
*/
?>
2¡¢ÉeÖÃÑeÖ¤¹aeÔo(controller)
<?php
//×¢ÒaÑeÖ¤¹aeÔoµÄ±aÁ¿Ãu±ØÐeÉeÖÃ³É config
$config = array(
array(
'field' => 'username',
'label' => 'Óû§Ãu',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'ÃÜÂe',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'È*ÈÏÃÜÂe',
'rules' => 'required|matches[password]'
),
array(
'field' => 'tel',
'label' => 'ÊÖ»u',
'rules' => 'required|integer|exact_length[11]'),
array(
'field' => 'email',
'label' => 'ÓÊÏa',
'rules' => 'required|valid_email'
)
);
//ÉÏÃaeµÄ»a×Ô¶¯
//µ¥¶ÀÉeÖùaeÔo
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
?>
3¡¢¹aeÔo¶ÔÓ¦µÄ´iÎoÌaʾ(controller)
<?php
$this->form_validation->set_message('required', '±ØÐeÌiд');
$this->form_validation->set_message('valid_email', '²»ÊÇÓÐЧµÄemail');
?>
4¡¢ÔËÐмi²e´iÎoÐÅÏ¢(controller)
<?php
$this->load->helper(array('form', 'url'));
//¼ÓÔØCI±iµ¥ÑeÖ¤¿a
$this->load->library('form_validation');
//----------------------------------------
# ÑeÖ¤¹aeÔo¼°´iÎoÐÅÏ¢´uÂe*ÅÔÚÕaÀi
//----------------------------------------
if ($this->form_validation->run() == FALSE){
//Ìa½»Ê§°Ü ÖØмÓÔرiµ¥²¿*Ö
$this->load->view('myform');
}else{
//Ìa½»³É¹¦ ±iµ¥´¦Ài
//Ìøת³É¹¦Ò³Ãae
$this->load->view('formsuccess');
}
}
5¡¢´iÎoÐÅÏ¢µÄÊa³oº¯Êý(view)
<?php
//1.Ò»¹ÉÄÔ¶uµÄÈ«²¿Êa³o(*ÅÔÚ±iµ¥±eÇ(C)µÄÉÏ*½¼´¿É)
echo validation_errors();
//2.Õe¶Ôµ¥¸o±iµ¥µ¥¶ÀÊa³o(*ÅÔÚµ¥¸o±eÇ(C)¸½½u ²ÎÊýΪ¶ÔÓ¦±iµ¥ÔªËصÄÓoÃu)
echo form_error('password');
//3.Õe¶Ôµ¥¸o±iµ¥Êa³oµÄʱºo ÐeÒªÐ޸Ķ¨½ç*u ÏÔʾ´iÎoÐÅÏ¢Ñuʽ£¨¿ØÖÆÆ÷ÀiÉeÖã(C)
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
//ÉeÖóÉÄÚÁªÔªËرȽϺÃ
?>
6¡¢´iÎoºo ÖØлØÌi±iµ¥(view)
<?php
//Ò»°aÔªËØ »ØÌi(*ÅÔÚ±eÇ(C)µÄvaluesÊoÐÔÖÐÊa³o)
echo set_value('email');
//ÌØÊaÔªËØselect/checkbox/radio µÚÈý¸o²ÎÊýΪtrueʱ ĬÈϱ»Ñ¡ÖÐ
//µÚ¶þ¸o²ÎÊý ÊǶÔÓ¦µÄ±iµ¥ÔªËصÄʵ¼ÊÖµ
echo set_select('myselect', 'three'); //*ÅÔÚoptionµÄ¿Õ°×ÊoÐÔÀi
echo set_checkbox('mycheck[]', '1'); //*ÅÔÚcheckboxµÄ¿Õ°×ÊoÐÔÀi
echo set_radio('myradio', '2'); //*ÅÔÚradioµÄ¿Õ°×ÊoÐÔÀi
?>
html´uÂe£º
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
<?php echo form_error('username'); ?>
<h5>Password</h5>
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<?php echo form_error('password'); ?>
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />
<?php echo form_error('passconf'); ?>
<h5>Email Address</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<?php echo form_error('email'); ?>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
¸u¶a¹ØÓÚCodeIgniterÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶codeigniterÈeÃŽ̡̳¡¢¡¶CI(CodeIgniter)¿o¼Ü½ø½×½Ì³Ì¡¡¢¡¶phpÓÅÐa¿ª*¢¿o¼Ü×ܽa¡¡¢¡¶ThinkPHPÈeÃŽ̡̳¡¢¡¶ThinkPHP³£Óý¨×ܽa¡¡¢¡¶Zend FrameWork¿o¼ÜÈeÃŽ̡̳¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡*
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚCodeIgniter¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£