Use $_POST in a Php variable

Asked

Viewed 913 times

0

I’m learning to use POO I would like to ask for help, as I cannot insert the $_POST within a variable, I’ve researched and looked at my books, but I’m not getting any progress. I’m grateful for your help in learning this the right way.

<?php
if (isset($_POST['enviarFrech'])) {
    $fech = $_POST['st'];
    $data_atual = date("Y-m-d");
    print_r($fech);
    echo $data_atual;

    class DadosMontagem {

        public $lojamont;
        public $monta;

        public function set_lojamontador($l) {
            $this->$lojamont = $l;
        }

        public function lsmt() {
            echo $l;
        }

    }

    $ls_mt = new DadosMontagem($_POST['lojamontar']);
    $ls_mt->set_lojamontador();
    $ls_mt->lsmt();
    fechamentoMontador($conexao, $fech, $ls, $mt, $data_atual);
}
?>
  • 2

    I don’t understand the problem.

  • Apparently you are using it right, the question is if you are sending it correctly...to know that you need to post the form that is sending the data.

  • You are passing the form data as " method='post' "?

  • Yes. I’m sending via method='post' I did an echo before class and displayed the data correctly, but inside the class I do wrong, I’m still learning to use POO so I’m sure I made silly mistakes from a beginner.

2 answers

3


Let’s break it up in stages:

  • Organizing: I suggest you declare the scope of the class first, only then to work on the scope outside of it. This approach leaves the code more organized.
  • Scope/safety: declare the attributes as private, this increases security as these are only visible in the scope of the class. To change the attributes, you can call the setNomeDoMetodo($argument), this allows you to perform validations within the method before changing the attribute. Already to read the attribute you use the getNomeDoMetodo()
  • Suggestive names: declares the methods and variables with short but suggestive names. This approach will make you not get lost in the code as it grows.

<?php

class DadosMontagem
{
    private $lojamont;
    private $monta;

    public function setLoja( $loja )
    {
            $this->lojamont = $loja;
    }

    public function getLoja()
    {
        return $this->lojamont;
    }
}

if(!empty($_POST['lojamontar']))
{
    $obj = new DadosMontagem;
    $obj->setLoja($_POST['lojamontar']);
    echo 'Data: ' . date("Y-m-d") . '<br>';
    echo 'Loja: ' . $obj->getLoja();
}
?>

<form method="post">
  <input type="text" name="lojamontar" />
  <button type="submit">Salvar Dados</button>
</form>
  • Fabio does not know why but I tested his code and he has the same behavior as what he had done. It only runs the $_POST outside the object. But it was useful for me to improve my way of building a class.

  • I added a form to see how it works and take as an example.

  • I really understand I did some testing including coming from another archive and it works perfectly. I will study more, thank you very much for your tips

1

Dude, if you’re not going through it’s because you must be missing the method form. No error, follow this logic:

<form method="post">
  <input type="text" name="nome_post" />
  <button type="submit">Salvar Dados</button>
</form>

<?php
if(!empty($_POST['nome_post'])){
  $post_ok = $_POST['nome_post'];
  echo $post_ok; //se imprimir é pq está passando corretamente
}
?>

Browser other questions tagged

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