zend表单提交按钮

时间:2013-10-28 09:23:07

标签: php forms zend-framework

我在zend中编写代码以从用户输入中捕获数据并将其保存在数据库中。我正在使用zend表单。要捕获的信息太多了,所以我创建了另一个表单来填写其他数据......第一个表单效果很好,但是当我按下第一个表单提交到数据库时,还要重定向到第二个表单我得到这个错误:  致命错误:在第114行的C:\ xampp \ htdocs \ Gforms2 \ application \ controllers \ IndexController.php中的非对象上调用成员函数setLabel()

当我在第二个表单中注释该行时,用户只能“查看并输入信息”,提交按钮不可见..请帮助

这是我的Forms,Indexcontroller和模型的代码

IndexController

<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $organization_details = new Application_Model_DbTable_OrganizationDetails();
        $this->view->organization_details= $organization_details->fetchAll();
    }

    public function addAction()
    {
        $form = new Application_Form_OrganizationDetails();

        $form->submit->setLabel('Next');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {                

                $Organization_Name = $form->getValue('Organization_Name');
                $Organization_Trading_Name = $form->getValue('Organization_Trading_Name');
                $Region = $form->getValue('Region');
                $City = $form->getValue('City');
                $Physical_Address = $form->getValue('Physical_Address');
                $Physical_code = $form->getValue('Physical_code');
                $Postal_Address = $form->getValue('Postal_Address');
                $Postal_code = $form->getValue('Postal_code');
                $Telephone_Number = $form->getValue('Telephone_Number');
                $Fax_Number = $form->getValue('Fax_Number');
                $Cellular_Number = $form->getValue('Cellular_Number');
                $Organization_Email = $form->getValue('Organization_Email');
                $Organization_Website = $form->getValue('Organization_Website');                                

                $organization_details = new Application_Model_DbTable_OrganizationDetails();
                $organization_details->addOrganizationDetails($Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website);

                $this->_helper->redirector('addform2');
            } else {
                $form->populate($formData);
            }

    }
    }

    public function editAction()
    {
        $form = new Application_Form_OrganizationDetails();
        $form->submit->setLabel('save'); //can set it here also in forms
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $Organization_Name = $form->getValue('Organization_Name');
                $Organization_Trading_Name = $form->getValue('Organization_Trading_Name');
                $Region = $form->getValue('Region');
                $City = $form->getValue('City');
                $Physical_Address = $form->getValue('Physical_Address');
                $Physical_code = $form->getValue('Physical_code');
                $Postal_Address = $form->getValue('Postal_Address');
                $Postal_code = $form->getValue('Postal_code');
                $Telephone_Number = $form->getValue('Telephone_Number');
                $Fax_Number = $form->getValue('Fax_Number');
                $Cellular_Number = $form->getValue('Cellular_Number');
                $Organization_Email = $form->getValue('Organization_Email');
                $Organization_Website = $form->getValue('Organization_Website');
                $organization_details = new Application_Model_DbTable_OrganizationDetails();
                $organization_details->updateOrganizationDetails($id, $Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $organization_details = new Application_Model_DbTable_OrganizationDetails();
                $form->populate($organization_details->getOrganizationDetails($id));
            }

    }
    }

    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $organization_details = new Application_Model_DbTable_OrganizationDetails();
                $organization_details->deleteOrganizationDetails($id);
            }
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $organization_details = new Application_Model_DbTable_OrganizationDetails();
            $this->view->organization_details = $organization_details->getOrganizationDetails($id);
        }
    }

   public function addform2Action()
    {
        $form = new Application_Form_PersonalDetails();

        $form->submit2->setLabel('Next');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {                

                $First_Name = $form->getValue('First_Name');
                $Last_Name = $form->getValue('Last_Name');
                $Telephone_Number = $form->getValue('Telephone_Number');
                $Fax_Number = $form->getValue('Fax_Number');
                $Cellular_Number = $form->getValue('Cellular_Number');
                $Email_Address = $form->getValue('Email_Address');
                $Username= $form->getValue('Username');
                $Password = $form->getValue('Password');
                $Confirm_Password = $form->getValue('Confirm_Password');                            

                $personaldetails= new Application_Model_DbTable_PersonalDetails();
                $personaldetails->addform2PersonalDetails($First_Name, $Last_Name, $Telephone_Number,$Fax_Number, $Cellular_Number, $Email_Address, $Username, $Password, $Confirm_Password);


                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }

    }
    }




}


form1

<?php
class Application_Form_OrganizationDetails extends Zend_Form
{
    public function init()
    {
        $this->setName('organization_details');
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');

        $Organization_Name = new Zend_Form_Element_Text('Organization_Name');
        $Organization_Name->setLabel('Organization Name')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Organization_Trading_Name = new Zend_Form_Element_Text('Organization_Trading_Name');
        $Organization_Trading_Name->setLabel('Organization Trading Name')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Region= new Zend_Form_Element_Select('Region');
        $Region->setAttribs(array('style' => 'width: 150px;'));  
        $Region ->setLabel('Region')
                ->addMultiOptions(array(
                            '--Select One--'=>'--Select One--',
                            'City of Johannesburg'=>'City of Johannesburg',
                            'City of Tswane'=>'City of Tswane',
                            'Ekhurhuleni Metropolitian Municipality'=>'Ekhurhuleni Metropolitian Municipality',
                            'West Rand District Municipality'=>'West Rand District Municipality',
                            'Sedibeng District Municipality '=>'Sedibeng District Municipality',

                        ));


        $City = new Zend_Form_Element_Select('City');
        $City->setAttribs(array('style' => 'width: 144px;'));  
        $City ->setLabel('City')
            ->addMultiOptions(array(
                            'Pretoria'=>'Pretoria',
                            'Gauteng'=>'Gauteng',
                            'Katlego'=>'Katlego',

                        )); 


        $Physical_Address = new Zend_Form_Element_Text('Physical_Address');
        $Physical_Address->setLabel('Physical Address')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Physical_code = new Zend_Form_Element_Text('Physical_code');
        $Physical_code->setLabel('Physical code')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Postal_Address = new Zend_Form_Element_Text('Postal_Address');
        $Postal_Address->setLabel('Postal Address')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Postal_code = new Zend_Form_Element_Text('Postal_code');
        $Postal_code->setLabel('Postal code')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Telephone_Number = new Zend_Form_Element_Text('Telephone_Number');
        $Telephone_Number->setLabel('Telephone Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Fax_Number = new Zend_Form_Element_Text('Fax_Number');
        $Fax_Number->setLabel('Fax Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Cellular_Number = new Zend_Form_Element_Text('Cellular_Number');
        $Cellular_Number->setLabel('Cellular Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Organization_Email = new Zend_Form_Element_Text('Organization_Email');
        $Organization_Email->setLabel('Organization Email')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('EmailAddress');

        $Organization_Website = new Zend_Form_Element_Text('Organization_Website');
        $Organization_Website->setLabel('Organization Website')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');




        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($id, $Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website, $submit));
    }
}

form2
<?php

class Application_Form_PersonalDetails extends Zend_Form
{

    public function init()
    {
        $this->setName('personaldetails');
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');

        $First_Name = new Zend_Form_Element_Text('First_Name');
        $First_Name->setLabel('First Name')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Last_Name = new Zend_Form_Element_Text('Last_Name');
        $Last_Name->setLabel('Last Name')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Telephone_Number = new Zend_Form_Element_Text('Telephone_Number');
        $Telephone_Number->setLabel('Telephone Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Fax_Number = new Zend_Form_Element_Text('Fax_Number');
        $Fax_Number->setLabel('Fax Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Cellular_Number = new Zend_Form_Element_Text('Cellular_Number');
        $Cellular_Number->setLabel('Cellular Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Email_Address = new Zend_Form_Element_Text('Email_Address');
        $Email_Address->setLabel('Email Address')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('EmailAddress');

        $Username = new Zend_Form_Element_Text('Username');
        $Username->setLabel('Username')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');


        $Password= new Zend_Form_Element_Text('Password');
        $Password->setLabel('Password')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Confirm_Password= new Zend_Form_Element_Text('Confirm_Password');
        $Confirm_Password->setLabel('Confirm Password')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');





        $submit2 = new Zend_Form_Element_Submit('submit');
        $submit2->setAttrib('id', 'submitbutton');

        $this->addElements(array($id, $First_Name, $Last_Name, $Telephone_Number,$Fax_Number, $Cellular_Number, $Email_Address, $Username, $Password, $Confirm_Password));
    }


}

model for form1 
<?php

class Application_Model_DbTable_OrganizationDetails extends Zend_Db_Table_Abstract
{

    protected $_name = 'organization_details';

    public function getOrganizationDetails($id)
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }

    public function addOrganizationDetails($Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website)
    {
        $data = array(
            'Organization_Name'=> $Organization_Name,
            'Organization_Trading_Name'=> $Organization_Trading_Name,
            'Region'=> $Region,
            'City'=> $City,
            'Physical_Address'=> $Physical_Address,
            'Physical_code'=> $Physical_code,
            'Postal_Address'=> $Postal_Address,
            'Postal_code'=> $Postal_code,
            'Telephone_Number'=> $Telephone_Number,
            'Fax_Number'=> $Fax_Number,
            'Cellular_Number'=> $Cellular_Number,
            'Organization_Email'=> $Organization_Email,
            'Organization_Website'=> $Organization_Website,
        );
        $this->insert($data);
    }

    public function updateOrganizationDetails($id, $Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website)
    {
        $data = array(
            'Organization_Name'=> $Organization_Name,
            'Organization_Trading_Name'=> $Organization_Trading_Name,
            'Region'=> $Region,
            'City'=> $City,
            'Physical_Address'=> $Physical_Address,
            'Physical_code'=> $Physical_code,
            'Postal_Address'=> $Postal_Address,
            'Postal_code'=> $Postal_code,
            'Telephone_Number'=> $Telephone_Number,
            'Fax_Number'=> $Fax_Number,
            'Cellular_Number'=> $Cellular_Number,
            'Organization_Email'=> $Organization_Email,
            'Organization_Website'=> $Organization_Website,
        );
        $this->update($data, 'id = '. (int)$id);
    }

    public function deleteOrganizationDetails($id)
    {
        $this->delete('id =' . (int)$id);
    }


}

model for form2

<?php

class Application_Model_DbTable_PersonalDetails extends Zend_Db_Table_Abstract
{

    protected $_name = 'personaldetails';

        public function getPersonalDetails($id)
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }

    public function addform2PersonalDetails($First_Name, $Last_Name, $Telephone_Number,$Fax_Number, $Cellular_Number, $Email_Address, $Username,$Password, $Confirm_Password)
    {
        $data = array(
            'First_Name'=> $First_Name,
            'Last_Name'=> $Last_Name,
            'Telephone_Number'=> $Telephone_Number,
            'Fax_Number'=> $Fax_Number,
            'Cellular_Number'=> $Cellular_Number,
            'Email_Address'=> $Email_Address,
            'Username'=> $Username,
            'Password'=> $Password,
            'Confirm_Password'=> $Confirm_Password,
        );
        $this->insert($data);
    }

    public function PersonalDetails($id, $First_Name, $Last_Name, $Telephone_Number,$Fax_Number, $Cellular_Number, $Email_Address, $Username,$Password, $Confirm_Password)
    {
        $data = array(
            'First_Name'=> $First_Name,
            'Last_Name'=> $Last_Name,
            'Telephone_Number'=> $Telephone_Number,
            'Fax_Number'=> $Fax_Number,
            'Cellular_Number'=> $Cellular_Number,
            'Email_Address'=> $Email_Address,
            'Username'=> $Username,
            'Password'=> $Password,
            'Confirm_Password'=> $Confirm_Password,
        );
        $this->update($data, 'id = '. (int)$id);
    }

    public function PersonalDetails($id)
    {
        $this->delete('id =' . (int)$id);
    }


}

0 个答案:

没有答案