How to access data from the form in object orientation

Asked

Viewed 98 times

0

How do I receive information coming from POST form, so that these data enter a decision structure, Example: I have an abstract class with the function of entering the system, but in the form the user has to mark if he is employee or delivery, the class that inherits the method enter before anything else need to know if this login is the employee of the company or the courier, as both will be directed to different screens.

PAGINA INDEX, NÃO COLOQUEI O FORMULARIO PARA NÃO FICAR EXTENSO DEMAIS.

require_once 'Log in.php';

$logging in = new Log in($user, $password); $logging ->setUsuario($_POST['usuario']); $logging->setSenha($_POST['password']);

PAGE LOGIN:

public function __construct($usuario, $senha) {
    $this->usuario = $usuario;
    $this->senha = $senha;

}

function getUsuario() {
    return $this->usuario;
}

function getSenha() {
    return $this->senha;
}

function setUsuario($usuario) {
    $this->usuario = $usuario;
}

function setSenha($senha) {
    $this->senha = $senha;
}


abstract public function Entrar($usuario, $senha);
abstract public function Sair();     
abstract public function Erro();

}

LOG IN

class Logar extends Login {

private $con;

public function __construct($usuario, $senha) {
    parent::__construct($usuario, $senha);
    $this->con = new Conexao();
}


public function Entrar($usuario, $senha) {
    parent::Entrar($usuario, $senha);
    echo "Método entrar esta funcionando";
}

public function Sair(){
    echo "Saindo";
}

public function Erro() {
    echo "Erro";
}

}

  • I believe that is not the correct form, since your class has to have a specific objective, at the time you create the object Logar, pass the data of $_POST['varx'] for the method Entrar

  • 1

    And don’t forget to use filter_input_array(INPUT_POST, FILTER_DEFAULT); to avoid Injection

  • 1

    Your logic is confused and it seems that you are learning POO and this is what is bothering you in this minimum context has no way to say what you need to do, usually has more things to do ( I think for the little I saw).

  • I’m starting to learn POO and have not found any article on the internet that teaches how to receive data from POST

1 answer

1

Try to leave your method like this:

public function Entrar() 
{
    echo $this->getUsuario() . ' - ' . $this->getSenha(); // Imprime usuário e senha recebidos p/ testar
}

In the object, in the scope where your code is being executed, you call the method Entrar() if the condition is true:

if (isset($_POST['botao']) && $_POST['botao'] == 'Login Funcionário')
{
    $obj = new Logar(); // Instancia o objeto da classe Logar

    $obj->setUsuario($_POST['usuario']); // Atribui usuário
    $obj->setSenha($_POST['senha']); // Atribui senha
    $obj->Entrar(); // Chama o método Entrar()
}

Don’t forget to process the data from $_POST.


EDIT:

$usuario and $senha are given from $_POST? You are assigning these values to these variables before?

If yes, then if you’re passing $usuario and $senha for the constructor method, so it is not necessary to call again: $logando->setUsuario($_POST['usuario']); and $logando->setSenha($_POST['senha']);, because there you call the __construct() of the mother class and already assigns values to the properties.

Another thing I found is in your method Entrar() of the class-daughter (Log in). In it you are calling the method Entrar() of the mother class which is abstract through the parent::Entrar($usuario, $senha);, that is, it has no functionalities implemented.

  • I understood Paulo but how do I get the variables coming from the form via POST? as I declare in the variables below that her values come from a form? $user = $_POST['user']; $password = $_POST['password']: This generates Error: Notice: Undefined index: usuario in C: xampp htdocs Examples:1 Logar.php on line 11

  • Whereas you have the Getters and Setters public in the mother class, you can do so with your object: $obj->setUsuario($_POST['usuario']); and $obj->setSenha($_POST['senha']);. This way, you will be able to access the attributes in the child class through the $this->getUsuario(); and $this->getSenha(); any method you wish, since $usuario and $senha sane protected.

  • I edited the answer, take a look to see if it helps you better.

  • It did not work by generating this error: Notice: Undefined variable: usuario in C: xampp htdocs Programas_orientacao_obj Casa_das_marmitas index.php on line 4 Notice: Undefined variable: in C: xampp htdocs Programas_orientacao_obj Casa_das_marmitas index.php on line 4 Notice: Undefined index: usuario in C: xampp htdocs Programas_orientacao_obj Casa_das_marmitas index.php on line 5 Notice: Undefined index: password in C: xampp htdocs Programas_orientacao_obj Casa_das_marmitas index.php on line 6

  • Put your updated code, please.

  • I edited the question code

  • I updated my answer. Take a look.

  • Now it worked out thank you was very well explained.

  • Good! Needing we are around.

Show 4 more comments

Browser other questions tagged

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