How to query DB without refresh and write the returned data?

0

I need to query my database with something like AJAX / JSON I'm not sure and return the result inside a div next to this # . div is already on the side to receive the result of the query.

I'm not sure how I can start doing this but I've created the PHP script that will query it:

<?php

require("conexao.php");

$estado     = $_GET['b_estado'];
$ret        = mysql_query("SELECT post_content FROM wp_posts WHERE post_type = 'estados' AND post_title = '$estado' AND post_status = 'publish'");

$array     = mysql_fetch_assoc($ret);
$dados     = $array['post_content'];

json_encode($dados);

?>

How do I get this action triggered with the following script in jQuery?

$("#map li a").on("click", function(e) {
e.preventDefault();

    var estado = $(this).attr("id");
    var b_estado = estado.toUpperCase();

});
    
asked by anonymous 28.10.2014 / 22:06

2 answers

1

Place this code inside some function that will be called to do what you want, remember to extract the data you want from the response.

I have not tested the code but this is basically what you have to do, anything you do adjustments, if you want I can give an improvement in the answer later:

var url = "seuArquivo.php";
var data = b_estado;
$.ajax({                 
        type: 'POST',                 
        //dataType: 'json',                 
        url: url,                 
        async: true,                 
        data: data,                 
        success: function(response) {
            $("#idSuaDiv").val(response);
            //vc pode fazer outras coisas aqui com a resposta
        }             
    });
    
28.10.2014 / 23:04
0

The following code allows the above query to be done in the database and returns the requested data through AJAX .

$("#map li a").on("click", function(e) {
e.preventDefault();

    // Recupero os dados para enviar para a consulta
    var estado = $(this).attr("id");
    var b_estado = estado.toUpperCase();

        // Faço uma requisição AJAX para tratar o script PHP
        $.ajax  ({  type: "POST", 
            url         : "consulta_estados.php",
            data        : { b_estado: b_estado },
            dataType    : "html",
            success     : function(result){ 
                $("#infos_atendimento").html(result);   
            }

    }); // AJAX

}); // CLICK

The PHP script that runs and returns the data is this:

<?php

header("Content-Type: text/html; charset=UTF-8", true);
require("conexao.php");

$estado     = $_POST['b_estado'];

if( isset($_POST['b_estado']) ){

    $ret            = mysql_query("SELECT post_content FROM wp_posts WHERE post_type = 'estados' AND post_title = '$estado' AND post_status = 'publish'");
    $contador       = mysql_num_rows($ret);
    $array      = mysql_fetch_assoc($ret);
    $dados      = $array['post_content'];       

if($contador > 0){  
    $resposta   = utf8_encode($array['post_content']);  
} else {    
    $resposta   = "Os dados solicitados não existem";   
}

echo $resposta;

}

?>
    
29.10.2014 / 15:29