Use $ _POST in a variable in Php

0

I'm learning to use POO I'd like to ask for help because I can not enter $_POST inside a variable, I've already researched and looked at my books, but I'm not getting any progress. I am grateful for the help in learning this in 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);
}
?>
    
asked by anonymous 27.10.2016 / 18:46

2 answers

3

Let's separate in steps:

  • Organization : I suggest that you declare the scope of the class first, only to work in scope out of it. This approach makes the code more organized.
  • Scope / security : Declare attributes as private , this increases security because they are visible only within the scope of the class. In order to change the attributes, you can call the setName ($ argument) , this allows you to perform validations inside the method before changing the attribute. Already to read the attribute you use the getNomeDoMetodo ()
  • Suggestive names : Declare methods and variables with short, but suggestive names. This approach will keep you from getting lost in 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>
    
27.10.2016 / 19:47
1

Dude, if it's not happening, you should be missing out on method of the form. There is 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
}
?>
    
27.10.2016 / 19:19