input with jquery sends value, but php does not query in BD any dirty?

Asked

Viewed 24 times

-1

    <script>
 jQuery(function($){                
                $('#busca-cid').submit(function(){                    
                    $.post('lista-cidades.php', $('#busca-cid input'), function(retorna){                        
                       
                        $("#cidades-atendidas").html(retorna);
                        
                    });
                    
                    return false;
                });
            });
</script>

 pagina php:

$buscar = filter_input(INPUT_POST, 'busca', FILTER_SANITIZE_STRING);
//$buscar = 'ca';
$paginas = filter_input(INPUT_POST, 'pagi', FILTER_SANITIZE_NUMBER_INT);
$qntd_result_pg = filter_input(INPUT_POST, 'qntd_pg', FILTER_SANITIZE_NUMBER_INT);
//calcular o inicio visualização


$ini = ($paginas * $qntd_result_pg) - $qntd_result_pg;

//consultar no banco de dados


 $result_cid = "SELECT cdstr_cidades.id, cdstr_cidades.cidade, cdstr_cidades.estado, cdstr_cidades.telefone, cdstr_cidades.dt_cadastro, cdstr_terminais.cid_term FROM cdstr_cidades INNER JOIN cdstr_terminais ON cdstr_cidades.id_terminal = cdstr_terminais.id WHERE cdstr_cidades.cidade LIKE '$buscar%' LIMIT $ini, $qntd_result_pg";
    $resultado_cid = mysqli_query($conn, $result_cid);

var_dump($buscar);

1 answer

0

Hello, it seems you are passing the DOM element as a parameter of the post, and not its value itself, which is what we want.

Pass it in the function with an object, to make sure that this value will be the same in the backend:

$.post(
     'lista-cidades.php', 
     { busca: $('#busca-cid input').val() },
     function(retorna){
         $("#cidades-atendidas").html(retorna);
     }
)

Thus, you will be sure that the element will be the same one you are trying to recover in PHP.

If it still doesn’t work, the error must be in the way you are referencing this element by javascript. Try:

console.log( $('#busca-cid input').val() )

So, you’ll be sure you’re taking the amount you want and sending it without being null or Undefined in your request.

I hope I’ve helped!!

Browser other questions tagged

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