Sending of ajax function variables

Asked

Viewed 23 times

0

I have an Ajax function, which when taking the focus of the discount field, it checks in the database and brings me an array of results via json, and with this result I check if the discount is larger than allowed and fill the discount field automatically. The function works fine when sending only the variable idproduto, when I send another variable together it doesn’t work. I put the console.log(modalidade)before $. ajax({` and the mode appears.

                        $.ajax({
                        url: 'buscadescontoproduto.php',
                        type: 'GET', // As variaveis são enviadas como $_GET;
                        dataType: 'JSON', // E o retorno dever ser feito em JSON;
                        data: {idproduto:idproduto, modalidade:modalidade}, // Variaveis;

                        beforeSend: function () {
                             //Pode ser colocado um loading aqui, exemplo;
                            //$('#desconto' + x).val('Carregando...');
                        },
                        success: function (data) {
                            console.log(data);
                           
                          },
                       });
                       

The php code where I get the variables:

<?php
    require('../Paginas/verificasession.php');
    if (isset($_GET['idproduto'])) {
     $idproduto = $_GET['idproduto'];
     $modalidade = $_GET['modalidade'];
    include '../Config/config.php';
    $return_arr = array();
    if ($con) {        
    $fetch = mysqli_query($con, "SELECT tabeladepreco.desconto 
                                        FROM produtos,tabeladepreco 
                                        where produtos.idproduto = $idproduto 
                                        AND tabeladepreco.idproduto = produtos.idproduto 
                                        AND tabeladepreco.modalidade = $modalidade 
                                        AND produtos.ativo = 1 ");

            while ($row = mysqli_fetch_array($fetch)) {

            $row_array['desconto'] = utf8_encode($row['desconto']);
            array_push($return_arr, $row_array);
        }
        mysqli_close($con);
        echo json_encode($return_arr);
        exit();
       }
     }
?>
  • Mr do not pass the mode, should give error in your php. Passing, depends on the type of the variable. If it is not numerical, it will also give error, by the way you interpolate the value of the variable in the query.

  • Friend, Understanding the error will be vital to define the problem the error can be your interpretation of what should happen, example of sending both with one and two parameters the result is the same. Also if the php answer comes empty or simply returns an exception. if you can clarify these points I can help

  • @Eduardoworrel when sending with only one variable works, the result comes, the Json response in php comes with the value, but when I send both, not the error , it comes empty.

1 answer

0


After reading the comments of @bfavaretto, I checked a way to pass the two numerical variables and so it worked.

Browser other questions tagged

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