Parameter by Ajax

Asked

Viewed 60 times

0

I need to pass some arguments through Ajax, at first the code presents syntax error.

            var comeco = $("data_filtroe").val();
            var fim = $("data_filtrod").val();    
            jQuery.ajax({
                type: "get",
                url: "status_relatorio.php",
                data: "comeco="+comeco+" && fim="+fim,
                success: function(data){
                    document.querySelector('#status_dia').innerHTML = data;
                }
            });

PHP code("status_report.php"):

<?php 
session_start();
$con = mysql_pconnect("localhost","root","eeep_#2017");
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
mysql_select_db("alunos",$con);
setlocale( LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese' ); 
date_default_timezone_set( 'America/Fortaleza' );
$data_inicio = $_GET['comeco'];
$data_fim = $_GET['fim'];
$data="";
for ($i=$data_inicio; $i <= $data_fim; $i++) { 
    if($data==""){
        $data = "data = ".$i;
    }else{
        $data = "and data = ".$i;
    }
}
if($data_inicio==$data_fim){
$data = "data=".$data_inicio;
}
$sql = "select * from ocorrencias where '$data' order by curso, nome_aluno asc";
$resultado = mysql_query($sql,$con);
$cont = 0;
$falta = 0;
$falta_justificada = 0;
$fardamento_incompleto = 0;
$es_autorizada = 0;
$indis = 0;
while($linha1 = mysql_fetch_array($resultado)){
    $cont++;
    if($linha1['tipo']=="Falta"){
        $falta++;
    }
    if($linha1['tipo']=="Falta Justificada"){
        $falta_justificada++;
    }
    if($linha1['tipo']=="Fardamento Incompleto"){
        $fardamento_incompleto++;
    }
    if($linha1['tipo']=="Entrada/Saída Autorizada"){
        $es_autorizada++;
    }
    if($linha1['tipo']=="Indisciplína"){
        $indis++;
    }
}
        echo "<table cellpadding='3' id='status_dia'>
            <tr>
                <td class='status'>Faltas: <label>".$data_inicio."</label></td>
                <td class='status'>Faltas Justificadas: <label>".$falta_justificada."</label></td>
                <td class='status'>Fardamentos Incompletos: <label>".$fardamento_incompleto."</label></td>
                <td class='status'>Entradas/Saídas Autorizadas: <label>".$es_autorizada."</label></td>
                <td class='status'>Indisciplína: <label>".$indis."</label></td>
                <td class='status'>Total: <label>".$cont."</label></td>
            </tr>
        </table>";?>

inserir a descrição da imagem aqui

  • It still didn’t work out. Error : Notice: Undefined index: comeco in C: xampp htdocs Sistema - Controle Geral de Alunos status_relatorio.php on line 11 Notice: Undefined index: fim in C: xampp htdocs Sistema - Controle Geral de Alunos status_relatorio.php on line 12

  • I did this... receive code... $data_start = $_GET['start']; $data_end = $_GET['end'];

  • @Have a look at my answer and my comments on it. Use $_POST["comeco"], $_POST["fim"].

  • Another thing... There are numerous errors in your code. The most serious is that you are using the extension mysql which has been completely removed in PHP 7 and deprecated since version 5.4 of it.

  • Another thing... It is much easier, simple, fast and practical you perform a separate consultation based on tipo de falta que você quer... Or use COUNT() and GROUP BY.

  • @Godfreytheking I can’t use the group by function because I need to show exactly all values.

  • Like I said... you can use COUNT() based on the type you want and group them. You have 5 different types, right? You just count the lines that have the tipo X and group in this type, accounts the lines that have the tipo Y and group together. In this way, SELECT will return only 5 lines.

Show 2 more comments

1 answer

1


According to the jQuery documentation you need to send data in format Plainobject, String or Array.

data Type: Plainobject or String or Array Data to be sent to the server. It is converted to a query string, if not already a string. It’s appended to the url for GET-requests. See processData option to Prevent this Automatic Processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes Multiple values with same key based on the value of the Traditional Setting (described Below).

Write the code below:

        // Supondo que "data_filtroe" é uma classe. 
        // Se for um ID, utilize $("#data_filtroe").val(); 
        var comeco = $(".data_filtroe").val();
        // Supondo que "data_filtrod" é uma classe.
        // Se for um ID, utilize $("#data_filtrod").val();    
        var fim = $(".data_filtrod").val();    
        jQuery.ajax({
            method: "POST",
            dataType: "json",
            url: "status_relatorio.php",
            data: { "comeco": comeco, "fim": fim },
            success: function(data){
                document.querySelector('#status_dia').innerHTML = data;
            }
        });

Remembering that:

  1. GET - Search, requires, requests data from somewhere specified.

  2. POST - Send the data to be processed somewhere specified.

  • It didn’t work, nothing came up.

  • data: { "comeco": comeco, "fim": fim } this snippet is sent the two variables to the PHP script status_relatorio.php. In PHP, you should recover the variables through $_POST["comeco"] and $_POST["fim"]. And finally, in your PHP file, to return some kind of information to the request Ajax, you must do echo json_encode("Olá mundo, estou saindo do PHP e voltando ao JS");

  • The code returns nothing when I use this code.

  • I edited my answer, take a look. And also post the PHP code in your question.

Browser other questions tagged

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