Các bước thực hiện :
1. Tạo file UserController.php , file này đặt theo đường dẫn sau nha : /var/www/Zend/application/controller , nội dung file như sau :
<?php
class UserController extends Zend_Controller_Action{
public function indexAction(){
$form=new Form_User;
if($this->_request->isPost()){
$name=$this->_request->getPost('name');
echo "Ten cua ban la :";
echo $name;
echo "</br>";
echo "Email cua ban la :";
$email=$this->_request->getPost('email');
echo $email;
echo "</br>";
echo "Gioi tinh cua ban la :";
$gender=$this->_request->getPost('gender');
echo $gender;
echo "</br>";
echo "Quoc gia cua ban la :";
$country=$this->_request->getPost('country');
echo $country;
echo "</br>";
echo " cm gia cua ban la :";
$note=$this->_request->getPost('note');
echo $note;
echo "</br>";
}
$this->view->form=$form;
}
}
2. Tạo file User.php , file này đặt trong thư mục : /var/www/Zend/application/forms , nội dung file như sau :
<?php
class Form_User extends Zend_Form{
public function init()
{
$this->setAction('')->setMethod('post');
$name=$this->createElement("text","name",array(
//"label" => "Full Name",
"size" => "30",
));
$email=$this->createElement("text","email",array(
//"label" => "Email",
"size" => "30",
));
$gender=$this->createElement("radio","gender",array(
//"label" => "Gender",
"multioptions"=> array(
"1" => "Male",
"2" => "Female",
),
));
$country=$this->createElement("select","country",array(
//"label" => "Country",
"multioptions"=> array(
"1" => "VietNam",
"2" => "Cambodia",
"3" => "Thai Lan",
)
));
$note=$this->createElement("textarea","note",array(
//"label" => "Note",
"cols" => "30",
"rows" => "5",
));
$submit=$this->createElement("submit","submit");
$this->setDecorators(array(
array('viewScript',
array('viewScript'=>'Form_Register.phtml'),
)));
$name->removeDecorator('HtmlTag')
->removeDecorator('Label');
$email->removeDecorator('HtmlTag')
->removeDecorator('Label');
$gender->removeDecorator('HtmlTag')
->removeDecorator('Label');
$country->removeDecorator('HtmlTag')
->removeDecorator('Label');
$note->removeDecorator('HtmlTag')
->removeDecorator('Label');
$submit->removeDecorator('DtDdWrapper');
$this->addElements(array($name,$email,$gender,$country,$note,$submit));
}
}
3. Tạo file Form_Register.phtml , file này đặt trong /var/www/Zend/application/views/scripts , nội dung file như sau :
<form action='<?php echo $this->element->getAction(); ?>'
method= '<?php echo $this->element->getMethod(); ?>'>
<p>Your Name:<br />
<?php echo $this->element->name; ?>
</p>
<p>Your Email:<br />
<?php echo $this->element->email; ?>
</p>
<p>Your Gender:<br />
<?php echo $this->element->gender; ?>
</p>
<p>Your Country:<br />
<?php echo $this->element->country; ?>
</p>
<p>Your Note:<br />
<?php echo $this->element->note; ?>
</p>
<p><?php echo $this->element->submit; ?>
</p>
</form>
4. Tìm file Bootstap.php trong thư mục : /var/www/Zend/application , thêm nội dung sau vào :
protected function _initAutoload(){
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => dirname(__FILE__),));
return $autoloader;
}
5. Tạo file index.phtml ,file này nàm trong thư mục :/var/www/Zend/application/views/scripts/user , nội dung file :
<?php
echo $this->form;
?>
<?php
echo $this->form;
?>
6. Demo : bạn mở trình duyệt lên và gõ vào: localhost/thư mục cài Zend Framework /user ,rồi enter xem kết quả .
7. Video demo :
0 nhận xét:
Đăng nhận xét