Pass PHP variable to JS via JSON

Asked

Viewed 381 times

0

I’m trying to show off the result of a select performed by PHP in an external file, in an HTML form. I am using JSON to receive variables, but nothing is displayed. Can someone help me figure out the mistake or explain me a better way to display the data, I’ve researched and found nothing!

Form page:

function Select_Produto(valor){
        IDProduto = valor;
        if(window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        }
        else if(window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = "PHP/Produto_SelectVar.php?valor="+valor;
        req.open("Get", url, true);
        req.onreadystatechange = function() {
        if(req.readyState == 4 && req.status == 200) {
            var resposta = JSON.parse(req.responseText);
               document.getElementById('Dados_cProd').value = resposta.Dados_cProd;
               document.getElementById('Dados_xProd').value = resposta.Dados_xProd;
               document.getElementById('Dados_NCM').value = resposta.Dados_NCM;
               document.getElementById('Dados_EXTIPI').value = resposta.Dados_EXTIPI;
               document.getElementById('Dados_CEST').value = resposta.Dados_CEST;
               document.getElementById('Dados_CFOP').value = resposta.Dados_CFOP;
               document.getElementById('Dados_uCom').value = resposta.Dados_uCom;
               document.getElementById('Dados_qCom').value = resposta.Dados_qCom;
               document.getElementById('Dados_vUnCom').value = resposta.Dados_vUnCom;
               document.getElementById('Dados_uTrib').value = resposta.Dados_uTrib;
               document.getElementById('Dados_qTrib').value = resposta.Dados_qTrib;
               document.getElementById('Dados_vUnTrib').value = resposta.Dados_vUnTrib;
               document.getElementById('Dados_vSeg').value = resposta.Dados_vSeg;
               document.getElementById('Dados_vDesc').value = resposta.Dados_vDesc;
               document.getElementById('Dados_vFrete').value = resposta.Dados_vFrete;
               document.getElementById('Dados_cEAN').value = resposta.Dados_cEAN;
               document.getElementById('Dados_cEANTrib').value = resposta.Dados_cEANTrib;
               document.getElementById('Dados_vOutro').value = resposta.Dados_vOutro;
               document.getElementById('Dados_vProd').value = resposta.Dados_vProd;
               document.getElementById('Dados_xPed').value = resposta.Dados_xPed;
               document.getElementById('Dados_nItemPed').value = resposta.Dados_nItemPed;
               document.getElementById('Dados_nFCI').value = resposta.Dados_nFCI;
               document.getElementById('Dados_NVE').value = resposta.Dados_NVE;
            }
        }
        req.send(null);
    }



insira o código aqui

Consultation page

echo  $array = array('Dados_cProd'=>$Dados_cProd,
                     'Dados_xProd'=>$Dados_xProd,
                     'Dados_NCM'=>$Dados_NCM,
                     'Dados_EXTIPI'=>$Dados_EXTIPI,
                     'Dados_CEST'=>$Dados_CEST,
                     'Dados_CFOP'=>$Dados_CFOP,
                     'Dados_uCom'=>$Dados_uCom,
                     'Dados_qCom'=>$Dados_qCom,
                     'Dados_vUnCom'=>$Dados_vUnCom,
                     'Dados_uTrib'=>$Dados_uTrib,
                     'Dados_qTrib'=>$Dados_qTrib,
                     'Dados_vUnTrib'=>$Dados_vUnTrib,
                     'Dados_vSeg'=>$Dados_vSeg,
                     'Dados_vDesc'=>$Dados_vDesc,
                     'Dados_vFrete'=>$Dados_vFrete,
                     'Dados_cEAN'=>$Dados_cEAN,
                     'Dados_cEANTrib'=>$Dados_cEANTrib,
                     'Dados_vOutro'=>$Dados_vOutro,
                     'Dados_vProd'=>$Dados_vProd,
                     'Dados_xPed'=>$Dados_xPed,
                     'Dados_nItemPed'=>$Dados_nItemPed,
                     'Dados_nFCI'=>$Dados_nFCI,
                     'Dados_NVE'=>$Dados_NVE);
    echo json_encode($array);

Modal of the Form

<div class="modal fade bs-example-modal-lg" id="MDProdutos" tabindex="-1" role="dialog" aria-labelledby="MDProdutosLabel">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="MDProdutosLabel">Produtos e serviços</h4>
                      </div>
                          <div class="modal-body">
                                <form class="row">
                                <div class="form-group">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>Código</label>
                                            <input type="text" class="form-control" id="Dados_cProd" >
                                        </div>
                                    </div>
                                    </div>
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>Descrição</label>
                                            <input type="text" class="form-control" id="Dados_xProd" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>NCM</label>
                                            <input type="text" class="form-control" id="Dados_NCM" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>EX TIPI</label>
                                            <input type="text" class="form-control" id="Dados_EXTIPI" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>CEST</label>
                                            <input type="text" class="form-control" id="Dados_CEST" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <label>CFOP</label>
                                        <select class="selectpicker form-control" id="Dados_CFOP">
                                          <option></option>
                                        </select>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>UN. Comercial</label>
                                            <input type="text" class="form-control" id="Dados_uCom" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Qtd. Comercial</label>
                                            <input type="text" class="form-control" id="Dados_qCom" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Valor. Unid. Comercial</label>
                                            <input type="text" class="form-control" id="Dados_vUnCom" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>UN. Trib.</label>
                                            <input type="text" class="form-control" id="Dados_uTrib" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Qtd. Trib.</label>
                                            <input type="text" class="form-control" id="Dados_qTrib" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Valor. Unid. Trib.</label>
                                            <input type="text" class="form-control" id="Dados_vUnTrib" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Tot. Seguro</label>
                                            <input type="text" class="form-control" id="Dados_vSeg" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Desconto</label>
                                            <input type="text" class="form-control" id="Dados_vDesc" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Tot. Frete</label>
                                            <input type="text" class="form-control" id="Dados_vFrete" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>EAN</label>
                                            <input type="text" class="form-control" id="Dados_cEAN" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>EAN Trib.</label>
                                            <input type="text" class="form-control" id="Dados_cEANTrib" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Outras Desp. Acessórias</label>
                                            <input type="text" class="form-control" id="Dados_vOutro" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Valor Tot. Bruto</label>
                                            <input type="text" class="form-control" id="Dados_vProd" >
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="checkbox">
                                            <label style="padding-top: 1.5rem;">
                                              <input type="checkbox" id="indTot"> Valor total bruto compõe o valor total dos produtos e serviços
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Pedido de compra</label>
                                            <input type="text" class="form-control" id="Dados_xPed" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Número do item do pedido de compra</label>
                                            <input type="text" class="form-control" id="Dados_nItemPed" >
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>Número de controle da FCI</label>
                                            <input type="text" class="form-control" id="Dados_nFCI" >
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">NVE</label>
                                            <input  class="form-control" id="Dados_NVE" >
                                        </div>
                                    </div>
                                </form>
                          </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                            <button type="button" class="btn btn-primary" data-dismiss="modal" onClick="Inserir_Produtos()">Salvar</button>
                      </div>
                    </div>
                  </div>
            </div>
  • Why instead of trying to display variable data with PHP you don’t try to display this data with jQuery even?

  • Because the data is the result of a Mysql query.

  • Enter the HTML code as well.

  • Puts a console.log in the variable resposta and put here those who appear.

  • Debug in the browser, in Chrome press F12 in the top tab select 'Network', and call the ajax request, there will appear the php file where the request was sent, there will have the headers that were sent, in the Answer tab if you can see error messages, and most likely where the error is.

  • VM2354:1 Uncaught SyntaxError: Unexpected token A in JSON at position 0&#xA; at JSON.parse (<anonymous>)&#xA; at XMLHttpRequest.Select_Produto.req.onreadystatechange (Produtos_e_servicos.php?id=8:4223)&#xA;Select_Produto.req.onreadystatechange @ Produtos_e_servicos.php? id=8:4223

  • In the console it appears that there is a syntax error in the json array, the data is loaded correctly, but I cannot identify the error. Array{"Dados_cProd":"12334556","Dados_xProd":"","Dados_NCM":"","Dados_EXTIPI":"","Dados_CEST":"","Dados_CFOP":"0","Dados_uCom":"","Dados_qCom":"0.0000","Dados_vUnCom":"0.0000000000","Dados_uTrib":"","Dados_qTrib":"0.0000","Dados_vUnTrib":"0.0000","Dados_vSeg":"0.00","Dados_vDesc":"0.00","Dados_vFrete":"0.00","Dados_cEAN":"","Dados_cEANTrib":"","Dados_vOutro":"0.00","Dados_vProd":"0.00","Dados_xPed":"","Dados_nItemPed":"","Dados_nFCI":"","Dados_NVE":""}

  • I updated with the form modal I’m trying to fill out.

  • Amigo has special characters inside this JSON. http://prnt.sc/dn4yy2

  • Your php encoding is UTF-8?

  • It’s in UTF-8 yes.

  • Guys, thank you so much for the force! It really was a problem in UTF-8 coding, I changed php code and it worked.

Show 7 more comments
No answers

Browser other questions tagged

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