Ajax warns success but does not insert data

Asked

Viewed 142 times

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?

  • 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)

  • if I try gives a console.log(data). within the Success says it is not set

  • do console.log(msg);

  • 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]

  • The string that appeared after the type, it appears in the URI but pq? if I’m using the POST method

  • as you are calling the function adicionarUsuario()? is a form?

  • Through the button inside the form

Show 3 more comments

1 answer

0


Modify the way the data is set in date ajax:

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

Should work.

  • It worked! Is there any way that the data I enter does not appear in the URI? whenever it is added like this: http://localhost/Base/additionUsuario.php? type=2&name=userDois&usuario=userDois&password=passwordDois

  • and the data that have been entered

  • and now, is entering the data but is not showing the Alert, wanted to show it, some hint?

  • Post your code with the modifications you made

  • Ready edited with the code working

  • Man, I put to run the ajax on my local machine and displayed the Alert of good. The only thing I did fix the space before < form method="POST"> and < /form>.

  • I noticed that here for some reason do not ask me which, when the error it goes pro Success, now that it is working and I put error: Function() { Alert("test") }, whenever it inserts it goes and gives the test Alert, but all right I really wanted to learn how to insert with ajax and I’m already working on!

Show 2 more comments

Browser other questions tagged

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