Receiving data in Controller

Asked

Viewed 280 times

-2

I’m trying to develop an MVC from scratch, without frameworks, however I’m having difficulties in receiving the input values via form in my controlller, I would like a help in this regard.

My form is as follows

Cadastrausuario.php

<form action="controller/UsuarioController.php?method=addUser" method="post" >
<div class="card" style="top:40px">
    <div class="card-header">
        <span class="card-title">Profissional</span>
    </div>
    <div class="card-body">
    </div>
    <div class="form-group form-row">
        <label class="col-sm-4 col-form-label text-right">Nome:</label>
        <input type="text" class="form-control col-sm-6" name="nome" id="nome" value="" />
    </div>
    <div class="form-group form-row">
        <label class="col-sm-4 col-form-label text-right">Login:</label>
        <input type="text" class="form-control col-sm-6" name="login" id="login" value="" />
    </div>
    <div class="form-group form-row">
        <label class="col-sm-4 col-form-label text-right">Senha:</label>
        <input type="password" class="form-control col-sm-6" name="senha" id="senha" value="" />
    </div>
    <div class="form-group form-row">
        <label class="col-sm-4 col-form-label text-right">Data de Nascimento:</label>
        <input type="date" class="form-control col-sm-6" name="data_nasc" id="data_nasc" value="" />
    </div>            
    <div class="card-footer">
        <input type="hidden" name="id" id="id" value="" />
        <button class="btn btn-success" type="submit">Cadastrar</button>
        <button class="btn btn-secondary" type="reset">Limpar</button>
        <a class="btn btn-danger" href="index.php">Cancelar</a>
    </div>
</div>

Usuariocontroller.php

class UsuarioController {
    $nome = $_POST['nome'];
public function addUser($nome){
    echo $nome;
    }
}

I tried several ways and could not. I wanted to know by passing the method adduser parameter, how to use the variable in the controller.

  • 1

    One of the few advantages of using MVC is catching a framework ready. I also don’t like frameworks, but since I’m going to do it in hand I do it in a simpler way than MVC, even more in PHP which is a language of script which is conducive to this. Anyway the question is not good. To say that you have done some things and that you are unable to go forward and now that delegating the solution to someone else seems to me out of scope. I didn’t close it because it’s on a thin line since at least it started doing something,but in practice it doesn’t have a specific problem yet, so it may be broad or unclear

  • Do the action of the form as action="controller/UsuarioController.php?method=addUser" will not cause the method to be executed addUser of your Controller. Nothing is magical. You will need to handle the request somewhere specific, receive this information and make the appropriate calls to the class and method. Other than that, your class UsuarioController did not make much sense. I recommend you stick to the basics, not only of PHP but mainly of the web concepts, before trying to build a framework own.

1 answer

3


it seems its function addUser is not called, just like the class UsuarioController is also not instantiated. Try changing your code to:

Cadastrausuario.php

<form action="controller/UsuarioController.php" method="post" >

Usuariocontroller.php

class UsuarioController{
    public function addUser($nome){
        echo $nome;
    }
}

$classeUsuarioController = new UsuarioController(); // instancia sua classe

if(isset($_POST['nome'])){ // Quando existir o post nome:
    $classeUsuarioController->addUser($_POST['nome']);
}



I hope I helped, hug!

  • 1

    I may not have been able to express my question properly, but that’s exactly what I wanted, thank you very much.

Browser other questions tagged

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