开发者

How can I save form input to a database, I'm having trouble sending the values to my controller

Here's my RegisterController:

public function saveforminformationAction(){
        $request = $this->getRequest();

        if($request->isPost()){
            //I NEED HELP WITH THE getFormValues() METHOD.
            $formResults = $this->getFormValues();
            $db = $this->_getParam('db');

            $data = array(
            'user'      => $formResults['username'],
            'email'     => $formResults['email'],
            'password'  => $formResults['password'],
            'passwordc' => $formResults['p开发者_Python百科asswordc'],
            'name'      => $formResults['name'],
            'avatar'    => $formResults['avatar'],
            );


            $db->insert('Usuario',$data);           
        }
    }

And here's my registration view:

<body>  
        <h1>Thanks for signing up!</h1> 
        <?php
        $this->form->setAction($this->url(array('controller' => 'registration','action'=>'saveforminformation'))); 
        $this->form->setMethod('post');
        echo $this->form;
        ?>


        <img alt="signupimg" src="/img/signup.png">
    </body>

I'm fairly new to Zend, but I'm eager to learn.

How can I get the values sent in the form?


You have to pass the data to the form before it can validate and filter it:

if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())
{
    $db->insert($form->getValues());
}

If the data is not valid then the form will render with messages that indicate to the user what's wrong.

Zend_From is an odd class. I think it's a victim of the tension between the 'only use what you want' ethos of the Zend Framework and the attempt to create a comprensive MVC framework. It performs controller duties (fetching user input, passing data to the view) and model duties (validating input).


The ZF Reference Guide on Zend_Form has all the info you need:

After a form is submitted, you will need to check and see if it passes validations. Each element is checked against the data provided; if a key matching the element name is not present, and the item is marked as required, validations are run with a NULL value.

Summing up from the examples in the reference guide:

    $form = new Your_Form;
    // check if we have a valid Form POST
    if($request->isPost() && $form->isValid($_POST)) {
        // get the filtered values from the Form
        $data = $form->getValues();
        // insert the values to the database
        $db->insert('Usuario', $data);
        // redirect and inform user of success
        $this->_helper->redirector(/*redirect somewhere*/);
    }
    // Set Form to View 
    // If it wasn't a POST, a blank Form will be shown in the View
    // If it's not a valid Form, the form will show old values and errors
    $this->view->form = $form;

The values returned by $form->getValues() will return all values entered into the form after they have been run through the form filters you set inside your form. If the form input names match the column names in your database, you do not have to map them to an appropriate array, before insertion to the database.


This is my 500th Answer! Yay!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜