Passing Parameter in URL using JSON + AJAX

Asked

Viewed 519 times

0

I am studying Javascript and would like a support from you.

I have a file I called getPagamentosMaioresQue.php, this file has the following syntax:

<!DOCTYPE html>
<html>
    <head>
    	<title> PHP Trabalhando com Arrays</title>
        <meta charset="utf-8">
    </head>

    <body>
        <?php require_once("Pagamentos.class.php");

            $valorPagamento = $_GET['valor'];

            $objPagamentos = new Pagamentos();
            $objPagamentos->pesquisaPagamentosMaioresQue($valorPagamento);

        ?>
    </body>

</html>

....and I have another file called grafico.php where I call the file above using this part of the code...

function desenhaGraficoPagamentos(){
                var jsonData = $.ajax({
                    url: "getPagamentosMaioresQue.php",
                    dataType: "json",
                    async: false
                }).responseText;

Turns out when I use the getPagamentosMaioresQue.php?valor=10 works normally....

I want to know how to pass these parameters to this file through the grafico.php?valor=10?

  • 1

    How is your.php graphics file included in the? require/include or <script page....?

  • I’m calling the.php graphical file is called directly...

2 answers

1

<!DOCTYPE html>
<html>
    <head>
        <title> PHP Trabalhando com Arrays</title>
        <meta charset="utf-8">
    </head>

    <body>
        <?php require_once("Pagamentos.class.php");
            $valorPagamento = filter_input(INPUT_POST,'VALOR',FILTER_DEFAULT);
            $objPagamentos = new Pagamentos();
            $objPagamentos->pesquisaPagamentosMaioresQue($valorPagamento);

        ?>
    </body>

</html>

function desenhaGraficoPagamentos(){
                var jsonData = $.ajax({
                    url: "getPagamentosMaioresQue.php",
                    async: false,
                    type: 'POST',
                    data: valor=400, 
                }).responseText;

0

function desenhaGraficoPagamentos(){
                var jsonData = $.ajax({
                    url: "getPagamentosMaioresQue.php",
                    dataType: "json",
                    async: false,
                    data: valor, 
                }).responseText;

add the date property in $.ajax where this variable (value) added as parameter in your $.ajax would be value received by your.php graphical file

  • Hello Sam, good morning. But how do I get the "value" variable used by the file getPagamentosMaioresQue.php?

  • you can put date: "<? php $_GET['value'] ?>"

  • in your getPagamentosMaioresQue.php file just take the data using one of these methods $_POST["date"]; or $GET["date"].

Browser other questions tagged

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