Query PHP data using the $.get json method and list data in an html

Asked

Viewed 1,996 times

1

I can do data entry but have no idea how to do data selection on a HTML using $.get JSON.

Php code

public function Inserir($tabela,$sql){            
     ksort($sql);
     $Campos_nome=  implode('`, `', array_keys($sql));
     $Campos_valor= ':'. implode(', :', array_keys($sql));
     $novo=$this->prepare("INSERT INTO $tabela ( `$Campos_nome`) VALUES ( $Campos_valor)");
     foreach ($sql as $key => $valor) {
         $novo->bindValue(":$key", $valor);
     }         
     $novo->execute();            
     if($novo->rowCount()>0){
         $novo->setFetchMode(PDO::FETCH_ASSOC);
         $valor=$novo->fetchAll(); 
         print json_encode($valor,JSON_PRETTY_PRINT);
     }            
}//fim

Then I do the serialization of mine Form send the data via POST to my archive php. Here is the code:

$("#novo").submit(function(){
    var url = $(this).attr('action');  
    var data = $(this).serialize();

    $.post(url,data,function(data){
        $('#result').append('<div>'+o.nome+'</div>');
        console.log(o.nome);
    });

    return false;
});

How do I return the values in a tabela or <li> using method $.get?

Note: I tried to do this using append but it doesn’t work properly:

function listar(){
  $.get(url,function(data){
     $('#div').append('<div>'+data.nome+'</div>');
     $('#div').append('<div>'+data.email+'</div>');
  )};
}
  • I didn’t understand your question, what do you mean "selection of data in an html"? It became somewhat vague... In case you want to filter the information coming in json?

  • You can put your HTML to make the question clearer?

  • @Sergio he needs to post PHP too, if he doesn’t have a return json header, it won’t work.

  • @Sam did an edit because as now I know to return an array so the code has to be that!!!

2 answers

2

In PHP file name = json.php

<?php
 ...
 .../*consulta ao banco de dados*/
$rows=array();
while($row = mysql_fetch_assoc($result))
 {
 $rows[]= $row;

}


header('Content-Type: application/json');
echo json_encode($rows);


?>

In Javascript :

  $.getJSON( "json.php", function( json ) {
  var items = [];
  $.each( json, function( key, val ) {
     items.push( "<li id='" + key + "'>" + val + "</li>" );
  });

  $( "<ul/>", {
   "class": "my-new-list",
    html: items.join( "" )
    }).appendTo( "body" );
  });

1

In place of the. name, place date.name, because, your return card is the date. Check if in return a json is coming, with json_encode in his PHP and add in your jQuery in the last configuration the word 'json'.

$("#novo").submit(function(){
    var url = $(this).attr('action');  
    var data = $(this).serialize();

    $.post(url, data, function (data) {
      $.each(data, function (index, value) {
        $('#result').append('<div>' + value.nome + '</div>');
        $('#result').append('<div>' + value.email + '</div>');
     });
    }, 'json');

    return false;
});

Browser other questions tagged

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