Ajax warns success but does not enter data

0

I already did 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 at the database the data was not there

Running JS Code

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>
    
asked by anonymous 20.10.2016 / 05:58

1 answer

0

Modify how data is set in ajax data :

date: {'type': type, 'name': name, 'user': user, 'password': password},

It should work.

    
20.10.2016 / 13:54