CI¿ò¼Ü±íµ¥Ñé֤ʵÀýÏê½â

5年以前  |  阅读数:831 次  |  编程语言:PHP 

±¾ÎÄʵÀý½²Ê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¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例