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();
}
}
there is some specific error?
– Reynnan Viktor
Other than by Ajax you can access by typing the address in the browser?
– bfavaretto
The error on the console is: 404?
– Aline
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.– edson alves