views:

51

answers:

1

Hi guys, I'm doing this tutorial: http://www.phpeveryday.com/articles/Zend-Framework-Database-Creating-Input-Form-P494.html

We are building a simple input form using POST and submitting it to a mySQL database. All is working fine. I'm just trying to get my head around the getRequest() function.

In the controller, we have this:

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

    $this->view->assign('action',"process");
    $this->view->assign('title','Member Registration');
    $this->view->assign('label_fname','First Name');
    $this->view->assign('label_lname','Last Name'); 
    $this->view->assign('label_uname','User Name'); 
    $this->view->assign('label_pass','Password');
    $this->view->assign('label_submit','Register');     
    $this->view->assign('description','Please enter this form completely:');        
}

and then in view:

 <form name="register" method="post" action="<?php echo $this->escape($this->action)?>">
  <table>
    <tr>
      <td><?php echo $this->escape($this->label_fname)?></td>
      <td><input type="text" name="first_name"></td>
    </tr>
    <tr>
      <td><?php echo $this->escape($this->label_lname)?></td>
      <td><input type="text" name="last_name"></td>
    </tr>   
    <tr>
      <td><?php echo $this->escape($this->label_uname)?></td>
      <td><input type="text" name="user_name"></td>
    </tr>   
    <tr>
      <td><?php echo $this->escape($this->label_pass)?></td>
      <td><input type="password" name="password"></td>
    </tr>   
  </table>
  <input type="submit" name="submit" value="<?php echo $this->escape($this->label_submit);?>">
  </form>

So what I don't understand is why do we need a getRequest() if I already have the method="post" and the action set? If I comment it out, the script doesn'twork. I see it's needed, but I don't understand why-especially since the $request variable doesn't seem to be used?

A: 

In the code you have provided, $request doesn't appear to be used at all. I don't see why commenting it out would break.

What happens, exactly, when you comment it out?

The getRequest() function is for getting the Request object, which gives you parameters and such (i.e. controller, action, etc);

EDIT:

I had a look at the tutorial, and it has this:

12   
13    $sql = "INSERT INTO `user`
14            (`first_name` , `last_name` ,`user_name` ,`password`)
15            VALUES
16            ('".$request->getParam('first_name')."', '".$request->getParam('last_name')."', '".$request->getParam('user_name')."', MD5('".$request->getParam('password')."'))";
17    $DB->query($sql);

You'll note that it uses the $request variable to get the Parameters: 'first_name', 'last_name', 'user_name', 'password'

And saves them into the DB.

Valorin
Commenting it out, I get the "An Error Occurred Application Error" message. uncomment it and it works.
Joel
@Valorin-the SQL code you just added is in a different method and view, and also contains it's own getRequest with $request.
Joel
Can you get the traceroute of the error?
Valorin
aw-jeez-I was making a stupid mistake-I wasn't clearing the fields when I was testing the commenting/uncommenting, and so it was trying to put duplicate content into the Db and getting an error. When I put in new content, I can comment that line out and things still work. Cool-one less confusing thing to deal with!
Joel
Ah cool :) Glad you sorted it out!
Valorin