Error in parameter passing of JQuery pro PHP

0

I'm making a modal that has its content according to the clicked link.

As the content is loaded from the DB, within the modal opening JQuery I made an Ajax call to a PHP. In this PHP I will access the database and return a table with the data. This table will be the modal content.

But I am not able to retrieve the parameter passed by Ajax in PHP.

Error:

Notice: Undefined index: idCategoria in C:\xampp\htdocs\canaa\gerarTabelaProduto.php on line 5

Notice: Undefined property: mysqli::$num_rows in C:\xampp\htdocs\canaa\gerarTabelaProduto.php on line 9

JQuery / Ajax:

$.ajax({
                    url: "gerarTabelaProduto.php",
                    type: "POST",
                    data: {
                        idCategoria: id
                    },
                    contentType: false,
                    cache: false,
                    processData:false,
                    success: function(data)
                    {
                        $("#conteudoModalProduto").html(data);
                    }
                });

PHP:

<?php

require_once "admin/conexao.php";

$idCategoria = $_POST["idCategoria"];
$nomeCategoria = "";

$categoria = $conexao->query("SELECT * FROM categoria WHERE idCategoria =".$idCategoria);
if($conexao->num_rows <> 0){
   $nomeCategoria = $categoria['nome'];
}

echo $nomeCategoria;
?>
    
asked by anonymous 27.05.2015 / 17:02

3 answers

1

Set the processData option to true:

$.ajax({
                    url: "gerarTabelaProduto.php",
                    type: "POST",
                    data: {
                        idCategoria: id
                    },
                    cache: false,
                    processData: true,
                    success: function(data)
                    {
                        $("#conteudoModalProduto").html(data);
                    }
                });
    
27.05.2015 / 19:16
1

Try to pass as string .

Another tip is to view the request by the browser. In the case of Chrome or Firefox you have the network tab that you can see the request and parameters being passed.

$.ajax({
                    url: "gerarTabelaProduto.php",
                    type: "POST",
                    data: {
                        'idCategoria': id
                    },
                    contentType: false,
                    cache: false,
                    processData:false,
                    success: function(data)
                    {
                        $("#conteudoModalProduto").html(data);
                    }
                });
    
27.05.2015 / 18:17
1

The problem is that you are passing an empty variable.

data: {idCategoria: id } // Esse id ta vindo de onde??? Ele não foi declarado

The correct is to get the value of this ID from some input or attribute of some html tag, for example: you said that you have a modal that will open with different content according to the link, so you can put it like this: / p>

<a href="#modal" class="btn-modal" id="<?php echo $id_categoria ?>">Abrir Modal</a> <!-- Acredito que você gere esses links dinamicamente. -->

Ai lah in ajax you get this value as follows:

 $(document).ready(function(){
    $('.btn-modal').click(function(){
        var id = $(this).attr('id'); // Agora sim a variável ID tem um valor

        $.ajax({
            url: "gerarTabelaProduto.php",
            type: "POST",
            data: {
                'idCategoria': id
            },
            contentType: false,
            cache: false,
            processData:false,
            success: function(data)
            {
                $("#conteudoModalProduto").html(data);
            }
        });
    });
});
    
29.05.2015 / 16:05