อันดับแรก ดูวิธีสร้างฐานข้อมูลและวิธีเชื่อมต่อกับฐานข้อมูลได้จากที่นี่ และให้สร้างไฟล์ขึ้นมาดังรูป
1.ให้สร้างไฟล์ user_controller.php ใน app/Controller เพราะเทเบิลในฐานข้อมูลของเราชื่อ users ครับ ให้พิมพ์Codeดังนี้
<?php class UsersController extends AppController { public $name = 'Users'; public $helpers = array("Html","Form"); public function register() { if ($this->request->is('post')) { if ($this->User->save($this->request->data)) { $this->Session->setFlash('สมัครสมาชิกเรียบร้อยแล้ว'); $this->redirect(array('action' => 'register')); } } } } ?>2.ให้สร้างไฟล์ user.php ใน app/Model ให้พิมพ์Codeดังนี้
<?php class User extends AppModel{ public $name = 'User'; public $validate = array( 'username' => array ( 'notEmpty'=>array( 'rule' => 'notEmpty','message'=>'กรุณากรอกUsername' ),'between'=>array( 'rule'=>array('between',5,10),'message'=>'กรอกUsernameระหว่าง 5 - 10 ตัวอักษรเท่านั้น'//,'allowEmpty' => true ),'isUnique'=>array( 'rule'=>'isUnique','message'=>'Username นี้มีผู้ใช้งานแล้ว' ) ),'password'=>array( 'notEmpty'=>array( 'rule' => 'notEmpty','message'=>'กรุณากรอกPassword' ),'minLength'=>array( 'rule'=>array('minLength', '5'),'message'=>'กรอกรหัสผ่านไม่น้อยกว่า 5 ตัวอักษร' ),'pass_equal'=>array( 'rule' =>'chkPassEqual','message' => 'รหัสผ่านทั้งสองช่องไม่ตรงกัน' ) ),'email'=>array( 'notEmpty'=>array( 'rule' => 'notEmpty','message'=>'กรุณากรอกอีเมล' ),'chkEmail'=>array( 'rule'=>array('email',true),'message'=>'กรุณากรอกอีเมลให้ถูกต้อง' ) ),'name'=>array( 'notEmpty'=>array( 'rule' => 'notEmpty','message'=>'กรุณากรอกชื่อสกุลให้ถูกต้อง' ) ),'tel'=>array( 'notEmpty'=>array( 'rule' => 'notEmpty','message'=>'กรุณากรอกเบอร์โทรศัพท์' ),'chkPhone'=>array( 'rule'=>'numeric','message'=>'กรอกเบอร์โทรศัพท์ไม่ถูกต้อง ตัวอย่าง 0823232323' ) ),'address'=>array( 'notEmpty'=>array( 'rule' => 'notEmpty','message'=>'กรุณากรอกที่อยู่ให้ชัดเจน' ) ) ); public function chkPassEqual(){//ตรวจสอบว่ารหัสผ่านทั้งสองช่องตรงกันหรือไม่ if($this->data['User']['password']==$this->data['User']['password_confirm']) { return true; } return false; } } ?>3.สร้างโฟลเดอร์ users และไฟล์ register.ctp ใน app/View ให้พิมพ์Codeดังนี้
<h2>สมัครสมาชิก</h2> <div style="float:left;width:450px;"> <?php echo $this->Form->create('User',array('action' => 'register'));?> <table> <tr> <td colspan="2"><?php echo $this->Form->input('username',array('label'=>'Username','style'=>'width:250px'));//หากไม่อยากให้แสดงlabel ให้ใช้คำสั่ง 'label'=>false?></td> </tr> <tr> <td><?php echo $this->Form->input('password',array('label'=>'รหัสผ่าน','type' => 'password'));?></td> <td><?php echo $this->Form->input('password_confirm',array('label'=>'ยืนยันรหัสผ่าน','type' => 'password'));?></td> </tr> <tr> <td colspan="2"><?php echo $this->Form->input('name',array('label'=>'ชื่อ-นามสกุล'));?></td> </tr> <tr> <td><?php echo $this->Form->input('tel',array('label'=>'เบอร์โทร'));?></td> <td><?php echo $this->Form->input('email',array('label'=>'อีเมล'));?></td> </tr> <tr> <td colspan="2"><?php echo $this->Form->input('address',array('label'=>'ที่อยู่','type'=>'textarea','escape' => false)); echo $this->Form->end(__('สมัครสมาชิก', true)); ?></td> </tr> </table> </div>เมื่อพิมพ์Codeทั้งหมดได้ถูกต้องแล้ว เวลาเรียกใช้งานให้พิมพ์URLดังนี้ http://localhost/cakephp/users/register จะได้ผลลัพธ์ดังนี้
ไม่มีความคิดเห็น :
แสดงความคิดเห็น