It is possible to return a vector to a javascript

Asked

Viewed 395 times

1

I’m developing customizable graphics by users where it can select the options they want, parameters to the php searching in the database sql and give me back what I want. For the charts I’m using highcharts. But I’m having trouble understanding how I return the vector of my function to js. I’m using jquery so I don’t need to refresh the page. But when I do

$.post('envia.php', {nome: nome}

It performs data search, but I do not know how to get this data back to my page where the graphics are generated. Someone would know the solution?

  • Want help with PHP or Javascript?

  • @Sergio Nos both, because I don’t know if it’s possible to return a vector to javascript and go through it normal. And some way to call the php file function by js.Obg

  • For Javascript the bfavaretto already answered, because the $.post jQuery accepts a function to return what PHP sends. For PHP you have to put more code so we can help... you have something already in PHP?

  • @Sergio didn’t, but it would be a select SELECT FROM products Where id=1; for example, it would return multiple records from there I wanted to go through the records to put on the chart

2 answers

7


The data comes back on callback of the method $.post:

$.post('envia.php', {nome: nome}, function(dados) {
    // use os dados aqui
    // o uso exato vai depender do formato que o PHP
    // usou para serializar os dados para envio
});

It looks like you want to return objects to JS, so PHP would have to send a JSON:

<?php
$vetor = array('x' => 10, 'y' => 20);

// envia dos dados para o cliente
header('Content-Type: application/json');
echo json_encode($vetor);
?>

Then you could use it like this:

$.post('envia.php', {nome: nome}, function(dados) {
    console.log(dados.x, dados.y);
});
  • I think I understand, to decode JSON with JQUERY is something +- like : array = jQuery.parseJSON(data);?

  • If the document is served with the correct content-type, jQuery identifies and already decodes the JSON for you (already decoded in callback).

2

The third parameter of the function post is a callback which receives server data as a parameter.

$.post('envia.php', {nome: nome}, function(dados) {
    console.log(dados);
});

On the server, you can do something like this:

$dados = array( ... );
echo json_encode($dados);
  • To decode JSON with JQUERY is something like : array = jQuery.parseJSON(data);?

  • @Rodolfooliveira Exact. You can also use the shortest form var resultado = $.parseJSON(dados); or you can also do the way @bfavaretto put it, by setting the response type in the header.

  • ,My biggest difficulty is this:on my console returned [{"iea_id":"1","man_ins_area_00":"0","man_ins_prod_00":"0","man_ins_area_05":"0",&#xA;"man_ins_prod_05":"0","man_ins_area_10":"0","man_ins_prod_10":"0","man_ins_area_13":"0",&#xA;"man_ins_prod_13":"0","id_cid":"1","id_cult":"1"},&#xA;{"iea_id":"2","man_ins_area_00":"0","man_ins_prod_00":"0","man_ins_area_05":"0", "man_ins_prod_05":"0","man_ins_area_10":"0","man_ins_prod_10":"0", "man_ins_area_13":"0","man_ins_prod_13":"0","id_cid":"2","id_cult":"1"}]" Two database records,has some way for me to access them separately,?

Browser other questions tagged

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