0
I already made an Insert without using ajax and it worked normal, when I used ajax and returned success I thought it was all right but when I went to look in the database the data were not there
JS Code Working
function adicionaUsuario(){
var tipo = $('#tipo').val();
var nome = $('#nome').val();
var usuario = $('#usuario').val();
var senha = $('#senha').val();
$.ajax({
method: 'POST',
url: "action/addUsuario.php",
data: {'tipo': tipo, 'nome': nome, 'usuario': usuario, 'senha': senha},
cache: false,
success: function(msg){
alert('Usuario inserido com sucesso');
}
});
}
action/addUsuario.php
<?php
require_once("../conexao.php");
require_once("../class/Usuario.php");
require_once("../class/UsuarioDao.php");
$nomeAdd = $_POST['nome'];
$usuarioAdd = $_POST['usuario'];
$senhaAdd = $_POST['senha'];
$tipoAdd = $_POST['tipo'];
$usuario = new Usuario($nomeAdd, $usuarioAdd, $senhaAdd, $tipoAdd);
$usuarioDao = New UsuarioDao($conexao);
$usuarioDao->adicionaUsuario($usuario);
DAO
function adicionaUsuario(Usuario $usuario) {
$usuario->setNome(mysqli_real_escape_string($this->conexao, $usuario->getNome()));
$usuario->setUsuario(mysqli_real_escape_string($this->conexao, $usuario->getUsuario()));
$query = "Insert Into usuario (nome,usuario,senha,tipo) values ('{$usuario->getNome()}','{$usuario->getUsuario()}','{$usuario->getSenha()}',{$usuario->getTipo()})";
return mysqli_query($this->conexao, $query);
}
Form
< form method="POST">
<div class="center" style="margin-top:10px;">
<h1 align="center"> Adicionar Usuário </h1>
<table class="table">
<tr>
<td><h3>Tipo: </h3></td>
<td> <select id="tipo" name="tipo" class="form-control" style="margin-top: 17px;">
<option value="2">Usuario</option>
<option value="1">Administrador</option>
</select> </td>
</tr>
<tr>
<td style="width: 20%;"><h3>Nome: </h3></td>
<td><input id ="nome"class="form-control" type="text" name="nome" style="margin-top: 17px;"></td>
</tr>
<tr>
<td style="width: 20%;"><h3>Usuario: </h3></td>
<td><input id ="usuario"class="form-control" type="text" name="usuario" style="margin-top: 17px;"></td>
</tr>
<tr>
<td style="width: 20%;"><h3>Senha: </h3></td>
<td><input id="senha" class="form-control" type="password" name="senha" style="margin-top: 17px;" required></td>
</tr>
</table>
<button type="submit" onclick="adicionaUsuario()" class="btn btn-warning btn-lg btn-block">Adicionar</button>
</div>
< /form>
In the add function, if you echo
$usuario->getNome()
for example, it prints the name?– Miguel
I wanted to know how to test this, because I don’t know how I can see getNome() using ajax pq the page won’t switch to the paginated action (I’m starting to learn how to use jquery, ajax)
– Reynnan Viktor
if I try gives a console.log(data). within the Success says it is not set
– Reynnan Viktor
do
console.log(msg);
– Miguel
It says "Undefined index name in action/addUsuario.php" "Undefined index user in action/addUsuario.php" "Undefined index password in action/addUsuario.php" here for better visualization> [http://prnt.sc/cwmjwh]
– Reynnan Viktor
The string that appeared after the type, it appears in the URI but pq? if I’m using the POST method
– Reynnan Viktor
as you are calling the function
adicionarUsuario()
? is a form?– Ricardo Moraleida
Through the button inside the form
– Reynnan Viktor