Ajax with PHP is not recognizing POST method index field

Asked

Viewed 41 times

-1

I’m making a system for restaurant orders, and I have a part to share commands. I have a javascript to create input commands I want to split, with maximum size of 3 command inputs

<script type="text/javascript">
    var linha = 1;
    function addInput(divName) {
        var newdiv = document.createElement('div');
        if (linha < 4) {
            newdiv.innerHTML = 'Comanda [' + linha + ']';
            newdiv.innerHTML += '<input type="text" id="ncomanda[' + linha + ']" name="DividirComanda[]">';
            newdiv.innerHTML += '<input type="text" id="scomanda[' + linha + ']" name="SenhaComanda[]">';

            document.getElementById(divName).appendChild(newdiv);
            linha++;
        }
    }




</script>

But I need to check the login and password of the command I want to split using AJAX.

My AJAX is like this

<script type="text/javascript">

            function alterar_div() {
                for (var i = 0; i < linha ; i++){
                var nome_comanda = $('#ncomanda['+i+']').val();
                var senha_comanda = $('#scomanda['+i+']').val();
                $.ajax({
                    type: "POST",
                    url: "verificaLogin.php",
                    data: {
                        nome_comanda: nome_comanda,
                        senha_comanda: senha_comanda
                    },
                    success: function (data) {
                        $('#conteudo').html(data);
                    }
                });
                }
            }

</script>

And my PHP file verifieLogin.php, is like this:

 <?php
$nome_comanda = $_POST['nome_comanda'];
$senha_comana = $_POST['senha_comanda'];

include "../Functions/conecta_mysql.inc";
//query para verificacao se usuario e senha conferem
$query = "SELECT * FROM comanda "
        . "WHERE nr_comanda='$nome_comanda' and senha_comanda='$senha_comanda'; ";

$resultado = mysqli_query($con,$query);

//Lê o número de linhas, se igual a ZERO, 
//usuario nao cadastrado ou senha
//nao confere

$linhas = mysqli_num_rows($resultado);

if ($linhas == 0) {
    echo "Invalida";
} else {
    echo 'Ok';
}



?>

However, by clicking the "check login" button that will call the AJAX function, it returns this error:

inserir a descrição da imagem aqui

Notice: Undefined index: nome_comanda in /opt/lampp/htdocs/Comandaelectronics/Client/verificaLogin.php on line 2

Notice: Undefined index: senha_comanda in /opt/lampp/htdocs/Commandelectronica/Client/verificaLogin.php on line 3

Notice: Undefined variable: senha_comanda in /opt/lampp/htdocs/Commandelectronica/Client/verificaLogin.php on line 8

Why aren’t you recognizing the variable index of the POST method in the verified fileLogin ? Ajax is not passing ?

  • And what is the result of var_dump($_POST)? You have checked in the developer tools, in the Network tab, if the AJAX request was made correctly?

  • var_dump returns array(0) { }

1 answer

1


$('#ncomanda['+i+']').val();
$('#scomanda['+i+']').val();

The above codes are returning "Undefined", which causes the variables not to be sent to PHP.

Check the selectors ;)

Browser other questions tagged

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