How to pass data from an HTML form to a php object

Asked

Viewed 1,152 times

-2

Good morning,

In the past articles I wrote I was mentioning that I was learning Object Orientation in PHP in the documentation itself, and now I decided to do some tests that maybe can be applied in real life, but there was no success.

I created a class in PHP called Person and an html form, but when I call the display method it cannot rescue the data that was passed.

class Pessoa {

    public $nome;
    public $idade;
    public $sexo;
    public $qualidade;
    public $defeito;

    public function __construct() {

        $this->nome = $nome;
        $this->idade = $idade;
        $this->sexo = $sexo;
        $this->qualidade = $qualidade;
        $this->defeito = $defeitos;

    }

    public function apresentacao() {

        echo("Olá! Meu nome é! $this->nome, eu possuo $this->idade anos.<br/>Sou do Sexo: $this->sexo");

    }


}

//Aqui é feito um teste para verificar se estava sendo pego realmente, está tudo ok!

echo $nome = $_POST['nome'];
echo $idade = $_POST['idade'];
echo $sexo = $_POST['sexo'];
echo $qualidade = $_POST['qualidade'];
echo $defeito = $_POST['defeito'];


$pessoa1 = new Pessoa();
$pessoa1->apresentacao();

HTML form

<form method="POST" action="Animal.php">

    <input type="text" class="" id="" name="nome">
    <input type="text" class="" id="" name="idade">
    <input type="text" class="" id="" name="sexo">
    <input type="text" class="" id="" name="qualidade">
    <input type="text" class="" id="" name="defeito">
    <button type="submit">Enviar</button>

</form>

Edit1: My intention is not to apply any advanced standard (file organization type, separate classes, etc.) I would just like to see where I am missing, my goal is to understand what is missing, what I am doing and what was in excess.

Edit2: The error you are giving in my PHP at show time is: that the variables are not defined.

Edit3: It was not for when I call the method apresentar() he already took and organized?

Notice: Undefined variable: in line name 15

Notice: Undefined variable: online age 16

Notice: Undefined variable: sex in line 17

Notice: Undefined variable: quality in line 18

Notice: Undefined variable: defects in line 19

  • 2

    echo $nome = $_POST['nome'], what this line should do?

  • Remembering that this is just a test (the entire file), but this line would indicate to check if you were really taking the form data.

  • Mainly because it is a test that the code should represent the reality of the question. You made a echo and a variable assignment on the same line. Did that make sense to you? What would be the expected result in this line, that is, what should be displayed by echo and what the value of $nome after running the line?

  • It really didn’t make much sense, thank you for the remark. To answer your question, it would be the form data, perhaps I did not understand very well.

2 answers

5


The problem is that you are not passing the variables to the class constructor, so they don’t exist. Try to do so:


public function __construct($nome, $idade, $sexo, $qualidade, $defeitos) {

        $this->nome = $nome;
        $this->idade = $idade;
        $this->sexo = $sexo;
        $this->qualidade = $qualidade;
        $this->defeito = $defeitos;

    }

And pass the form data to the class:


echo $nome = $_POST['nome'];
echo $idade = $_POST['idade'];
echo $sexo = $_POST['sexo'];
echo $qualidade = $_POST['qualidade'];
echo $defeito = $_POST['defeito'];


$pessoa1 = new Pessoa($nome, $idade, $sexo, $qualidade, $defeito);

$pessoa1->apresentacao();

  • Oops! It worked @Alexsanderss, but I was in doubt now, when I saw in the PHP documentation it did not pass the parameters inside the () in the constructor, could you explain to me the pq should pass? Same in new Person

  • 1

    When you do $this->nome = $nome means that the variable $nome of the object being instantiated (in this case pessoa1) will receive the variable value $nome of the constructor that did not exist until then because it had not been declared. Already when you pass to the new Pessoa you are passing the value of the variable $nome to the constructor, then pass the value to the object variable. In short: the value of $nome is passed to the constructor of the class, which then passes to the variable of the object.

1

First, the problem in the constructor method of its class:

public function __construct() {
    $this->nome = $nome;
    $this->idade = $idade;
    $this->sexo = $sexo;
    $this->qualidade = $qualidade;
    $this->defeito = $defeitos;
}

You are assigning the fields of your object the variables $nome, $idade, $sexo, $qualidade and $defeitos, but these variables do not exist in the scope. Imagine the method/function as a black box. In it you can only work with what you explicitly put in the box. There’s no way you can access a variable within the method you didn’t add explicitly the scope of the method. The variables you defined outside the class even have the same name, but they are defined in different scopes and have no relation to each other.

If you’ve got it right, the central word is: scope. Study more in depth how PHP manages the scopes and when you understand this part well you will be able to solve the vast majority of problems you encounter on your own. Knowing how to manipulate the language scopes in your favor gives you a very big advantage over the language and understanding how it works is a fundamental part of using it correctly.

The easiest way to import an external variable into an internal scope is through the function parameters. That is, if you have five variables outside the class and need to pass inside the method, then you need to set the parameters:

public function __construct($nome, $idade, $sexo, $qualidade, $defeitos) {
    $this->nome = $nome;
    $this->idade = $idade;
    $this->sexo = $sexo;
    $this->qualidade = $qualidade;
    $this->defeito = $defeitos;
}

When studying about scopes you, sooner or later, will cross with the directive global, which also serves to import variables outside an internal scope, but I assume that it does not serve for this example.

Also, since the fields in your class have public visibility, you can set their values directly:

$pessoa = new Pessoa();

$pessoa->nome = $nome;
$pessoa->idade = $idade;
$pessoa->sexo = $sexo;
$pessoa->qualidade = $qualidade;
$pessoa->defeitos = $defeitos;

However, note that in this way no parameter is passed to the method at class initialization. In fact, in doing so, you wouldn’t even need a defined builder:

class Pessoa {

    public $nome;
    public $idade;
    public $sexo;
    public $qualidade;
    public $defeito;

    public function apresentacao() {
        echo("Olá! Meu nome é! $this->nome, eu possuo $this->idade anos.<br/>Sou do Sexo: $this->sexo");
    }

}

But there are well-defined reasons why people avoid doing so and, to summarize, because there is no guarantee that the object will be created in a valid state - with the constructor method yes, given that this is exactly his responsibility.

Another detail is that, although it works normally, do echo $nome = $_POST['nome'] doesn’t make any sense. In a line of code you assign a variable and display it as a form of debug. A line of code with two responsibilities. Avoid doing this. If the idea was just to check that what was typed in the form is coming in correctly, do something like:

$nome = $_POST['nome'];
$idade = $_POST['idade'];
...

var_dump($_POST);

So you’ll see everything that came in PHP from the form. Another way much better than this is to configure your server to work with debug tools such as Xdebug. With this tool you can analyze the values and contexts throughout your code without having to modify it to display on the page.

  • I was surprised by the reply @Anderson Carlos Woss, ended up clarifying a lot that I was in doubt, because I’m starting in this web world "currently", and I don’t know very well the good and bad practices that I should apply, I’m following the pure documentation so sometimes I get lost.

Browser other questions tagged

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