Ajax running Controller (MVC, PHP)

Asked

Viewed 1,495 times

6

I just started to give a studied in mvc, and I have a question. How to make my ajax execute certain method of my controller. obs. I’m not using any framework, below is the code.

View

<div class="form-cadastro">

    <input type="text" class="form-control" id="nome" placeholder="Nome" required autofocus>
    <input type="text" class="form-control" id="nome_usuario" placeholder="Nome de Usuário (Apelido)" onkeydown="Mask(this,user);" onkeypress="Mask(this,user);" onkeyup="Mask(this,user);" maxlength="20">
    <input type="text" class="form-control" id="nascimento" placeholder="Data de Nascimento" onkeydown="Mask(this,DataM);" onkeypress="Mask(this,DataM);" onkeyup="Mask(this,DataM);" maxlength="10">
    <input type="email" class="form-control" id="email" placeholder="Email" required autofocus>
    <input type="password" class="form-control" id="senha" placeholder="Senha" required>

    <div class="termos text-center">
        <a data-toggle="modal" data-target="#modal-termos">Termos de Uso</a>
    </div>

    <div class="btn btn-lg btn-primary" id="btn-cadastro">Aceitar os termos de uso e Cadastrar</div>

  </div>

js

$.ajax({
        type: 'post',
        data:{
            nome: nome,
            username: username,
            nascimento: nascimento,
            email: email,
            senha: senha
        },
        dataType: 'json',
        url: 'controller/cadastro/index.php',
        success: function(msg){

            if(msg.erro == 0){

                window.location.href = "dashboard";

            }else{

                swal('Não foi possível cadastrar');
                fnLoading(false);
                return;

            }

        }

    })

controller

class cadastro{

    public function cadastrar(){

        $player = new Player();

        $player->nome       = mysql_escape_string($_POST['nome']);
        $player->username   = mysql_escape_string($_POST['username']);
        $player->nascimento = mysql_escape_string($_POST['nascimento']);
        $player->email      = mysql_escape_string($_POST['email']);
        $player->senha      = mysql_escape_string($_POST['senha']);

        $player->inserir();

    }

}
  • 1

    there is some specific error?

  • Other than by Ajax you can access by typing the address in the browser?

  • The error on the console is: 404?

  • Without framewok you will probably have to create a router, which receives all the requests and passes to the corresponding files... Could be in index, you would pass in the url something like: controller/index.php?rota=cadastro and in the index would call the corresponding class.

1 answer

1

The mistake won’t be in the url in the ajax?

You have:

url: 'controller/cadastro/index.php'

Should stay:

url: '_caminho_/cadastro/cadastrar'

Being that the link to call one controller follows the following pattern:

Caminho/Controller/Method/

Being:

  • Caminho : Corresponds to the path to the controller;
  • Controller: Controller name;
  • Method: Method to be used within the controller.

But if you want to work with MVC, I recommend using a framework, like Laravel or Codeigniter.

Browser other questions tagged

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