I can not get the value of an input to compare inside a _construct

1

My project registers users and in the database I have a table User and another Profile . The User is for login and password data, and Profile for personal data. But now I need to create a new type of Profile , getting ProfileEmpresa and ProfileCliente . In the registration form you have input hidden saying what type of profile (1 or 0), to direct the data to the right table.

The problem: in class Registrationcontrollerextention I can not get the value of 'type' and make a if/else within method construct .

UPDATE: I've been able to resolve this problem, but even writing to BD correctly displays an error on the page. Notice: Undefined variable: type in C: \ xampp \ htdocs \ parties \ controllers \ authenticate \ registrationcontrollerextention.php on line 24

class Registrationcontrollerextention{

private $registry;
private $extraFields = array();
private $errors = array();
private $submittedValues = array();
private $sanitizedValues = array();
private $errorLabels = array();
private $tipo;

public function __construct($registry )
{
    if(isset($_POST['register_tipo']))
        {
            $tipo = $_POST['register_tipo'];
            echo $tipo;
        }
          /* essa eh a linha 24 */
        if($tipo == 0)
        {
        $this->registry = $registry;
        $this->extraFields['dino_name'] = array( 'friendlyname' => 'Pet Dinosaurs Name', 'table' => 'profileCliente', 'field' => 'dino_name', 'type' => 'text', 'required' => false );
        $this->extraFields['dino_breed'] = array( 'friendlyname' => 'Pet Dinosaurs Breed', 'table' => 'profileCliente', 'field' => 'dino_breed', 'type' => 'text', 'required' => false );
        $this->extraFields['dino_gender'] = array( 'friendlyname' => 'Pet Dinosaurs Gender', 'table' => 'profileCliente', 'field' => 'dino_gender', 'type' => 'text', 'required' => false);
        $this->extraFields['dino_dob'] = array( 'friendlyname' => 'Pet Dinosaurs Date of Birth', 'table' => 'profileCliente', 'field' => 'dino_dob', 'type' => 'DOB', 'required' => false );
        }
        else
        {
        $this->registry = $registry;
        $this->extraFields['dino_name'] = array( 'friendlyname' => 'Pet Dinosaurs Name', 'table' => 'profileEmpresa', 'field' => 'dino_name', 'type' => 'text', 'required' => false );
        $this->extraFields['dino_breed'] = array( 'friendlyname' => 'Pet Dinosaurs Breed', 'table' => 'profileEmpresa', 'field' => 'dino_breed', 'type' => 'text', 'required' => false );
        $this->extraFields['dino_gender'] = array( 'friendlyname' => 'Pet Dinosaurs Gender', 'table' => 'profileEmpresa', 'field' => 'dino_gender', 'type' => 'text', 'required' => false);
        $this->extraFields['dino_dob'] = array( 'friendlyname' => 'Pet Dinosaurs Date of Birth', 'table' => 'profileEmpresa', 'field' => 'dino_dob', 'type' => 'DOB', 'required' => false );

        }
}

This is my form

    <form action="authenticate/register" method="post" class="form-horizontal"  role="form"> 

                        <input type="text" class="form-control"  id="register_user" name="register_user" value="{register_user}" >

                        <input type="password" class="form-control"  id="register_password" name="register_password" value="" >

                        <input type="password" class="form-control"  id="register_password_confirm" name="register_password_confirm" value="" >

                        <input type="text" id="register_email" name="register_email" value="{register_email}" class="form-control" />

                        <input type="text" id="register_dino_name" name="register_dino_name" value="{register_dino_name}" class="form-control"/>

                        <input type="text" id="register_dino_breed" name="register_dino_breed" value="{register_dino_breed}" class="form-control" /><br /> 

                        <input type="text" id="register_dino_dob" name="register_dino_dob" value="{register_dino_dob}" class="form-control"/> 

                        <input type="submit" id="process_registration" name="process_registration" value="Enviar!" class="botao"/> 

                        <input type="hidden" id="register_tipo" name="register_tipo" value="1"/>
            </form> 
    
asked by anonymous 25.09.2014 / 22:40

2 answers

1

Error response:

  

Notice: Undefined variable: type

Note that the variable $tipo is only being set if $_POST['register_tipo'] is sent.

You can use the form below and verify that the field has been sent, otherwise you can fire an Exception . Use elseif if you have more than two options. You can also use switch when you have more options to check.

public function __construct($registry )
{
    // verifica se o campo foi enviado
    if( ! isset( $_POST['register_tipo'] ) ){
        throw new Exception( "O campo 'register_tipo' não foi enviado" );
    } else {
        // verifica o tipo
        if( $_POST['register_tipo'] === 'um valor qualquer' ){
            // ...
        } elseif ( $_POST['register_tipo'] === 'outro valor qualquer' ){
            // ...
        }
    }
}

Just as an illustration, I used the identical comparison operator (===). $_POST returns a string, so $_POST['register_tipo'] === 0 will fail because typing is different.

    
26.09.2014 / 04:36
0

As @PapaCharlie has already said, the error Undefined variable is caused because the local variable $tipo may not be properly defined if the code within IF is not executed. p>

However, if the idea is to put register_tipo in the private $tipo attribute of the class, then make $this->tipo , because unlike other languages like Java, in PHP you do not access attributes directly.

But since the posted form has the field name with value register_tipo , the problem should not occur. Make sure $_POST is actually getting this value.

It seems that the code would work.

    
26.09.2014 / 16:46