I can’t get the value of an input to compare within a _Construct

Asked

Viewed 845 times

1

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

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

UPDATE: I was able to solve this problem, but even recording correctly in the comic shows an error on the page. Notice: Undefined variable: tipo in C: xampp htdocs festas 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> 
  • puts the source of your form also so we can analyze better

  • I managed to. Thank you!!

  • @Rachel, I’m glad you solved it. If it was with any response, you can check, otherwise you can describe the solution to anyone who might have the same problem as you.

2 answers

1


Reply concerning the error:

Notice: Undefined variable: type


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

You can use the form below and check if the field has been sent, otherwise you can fire a 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, logo, $_POST['register_tipo'] === 0 will fail because the typing is different.

0

As @Papacharlie said, the mistake Undefined variable is caused because the local variable $tipo may not be properly defined if the code within the IF not executed.

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

But as the posted form has the field name worthwhile register_tipo, the problem should not occur. Check if the $_POST is actually getting that amount.

Otherwise, looks like that the code would work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.