Repeated post in sending the data to the controller, how do I fix?

Asked

Viewed 219 times

0

I’m trying to develop a php mvc system but I came across this problem in Create. The post is sending multiple data repeated do not know why this is happening.

inserir a descrição da imagem aqui

My view create looks like this

<form id="form1" action="<?php echo BASE_URL; ?>Products/Create" method="post">
    <div class="form-group">
        <label for="exampleInputEmail1">Nome</label>
        <input type="text" value="" class="form-control" name="name" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
    </div>

    <button type="submit" class="btn btn-success">Cadastrar</button><br><br>
</form>

There’s only one field because I was testing, now my way in the controller

public function Create(){
        $this->view->title = 'Novo';
        $this->view->dados = $_POST;

        if($_POST){
            //$this->view->errors = 'Não é um número';

            var_dump($_POST);

            $result = $this->Create($_POST['name']);

            if($result == true){
                $this->Redirect('Index');
            }

            $this->view->Render('Create');
            exit;
        }
        var_dump($_POST['name']);
        //$this->view->dados = $_POST;



        $this->view->Render('Create');
    }

I put the var_dump was then I saw why it was wrong in the Internet, it’s multiplying the post now I do not understand why.

1 answer

1

The post is not being sent several times, you are reading it several times, by calling a function within the function itself, creating an infinite loop:

You have a function Create and within it calls Create:

public function Create(){
    //...
   $result = $this->Create($_POST['name']);
    //...
}

The way your code is not making much sense, note that you create the function Create but it does not receive any parameter, but within it you call the same function by passing a variable

  • Truth, fixed the error, I was not connecting the model in the Create method

  • If you have solved the problem mark the answer as correct :)

Browser other questions tagged

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