Antonio D'Angelo
Antonio D'Angelo

Reputation: 119

PHP, jQuery and JSON, date search

I have this situation:

All the search i do via the form works perfectly, but when i set dates it return nothing...also if query is valid.. any idea ?

JS PAGE

$( '#frm_ricerca' )
    .submit( function( e ) {
        console.log('fired');
        $("#ricerca_btn").html('Caricamento in corso...');
        table.clear().draw();
        ZEUS.Notification('bottom', 'center', 'Ricerca Avviata', 1);
        e.preventDefault();
        $.ajax( {
            url: 'index.php?dispatch&token=XXXX&method=search_by',
            type: 'POST',
            data: new FormData( this ),
            processData: false,
            contentType: false,
            success : function(datas){
              $.each (datas, function(i, v){
                 table.row.add([v.fieldA, v.fieldB...]);
              });
                $("#count_research").text(datas.length + ' Elementi trovati');
                $("#search_result").show();
                table.draw();
                $("#ricerca_btn").html('Ricerca');

            },
            error : function(e){
                console.log("---- errore ----" );
                console.log(e);
                console.log("--- FINE ERRORE ---");
                $("#ricerca_btn").html('Ricerca');
            }
        } );
    } );

PHP PAGE

$searchKeys = array(
        'id_operazione'       => filter_input(INPUT_POST, 'reg_id', FILTER_SANITIZE_STRING),
        'rapporto_numero'     => filter_input(INPUT_POST, 'rapporto_numero', FILTER_SANITIZE_STRING),
        'CONCAT(Cognome, \' \', Nome) '  => filter_input(INPUT_POST, 'nominativo', FILTER_SANITIZE_STRING),
        'CodiceFiscale'  => filter_input(INPUT_POST, 'cf', FILTER_SANITIZE_STRING),
        'data_formazione >= '  => filter_input(INPUT_POST, 'data_dal', FILTER_SANITIZE_STRING),
        'data_formazione <= '  => filter_input(INPUT_POST, 'data_al', FILTER_SANITIZE_STRING),
    );


    $mainQuery = 'SELECT id_operazione, Cognome, Nome, CodiceFiscale, data_formazione FROM database WHERE ';

    foreach ($searchKeys as $field=>$value) {
        if(!empty($value)){
            if (strpos($field, 'Cognome') !== false) {
                $mainQuery .= $field . " LIKE '%$value%' AND ";
            } else if(strpos($field, 'data') !== false) {
                $mainQuery .= $field . " '$value' AND ";
            } else {
                $mainQuery .= $field . " = '$value' AND ";
            }
        }
    }

    $mainQuery = substr($mainQuery, 0, -4);

    $result = $mysqli->query($mainQuery);
    $data = array();
    if($result) {
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }

        if(empty($data)){
            http_response_code(200);
            echo json_encode(array("status"=>"succes", "message"=>"no data"));
        } else {
            http_response_code(200);
            echo json_encode($data);

        }
    } else{
        http_response_code(400);
        echo json_encode(array("status"=>"error", "query"=>$mainQuery));
    }

So, basically my problem is that whatever param i use for the search it works...

BUT if i set 1 or both dates it give me response 200 and no data....

any ideas ?

-- UPDATE --

Query formed used dates

SELECT id_operazione, Cognome, Nome, CodiceFiscale, data_formazione FROM aui WHERE data_formazione >= '2017-01-01'

weird

It return me error with status 200....thats even more weird

Upvotes: 0

Views: 97

Answers (1)

Antonio D&#39;Angelo
Antonio D&#39;Angelo

Reputation: 119

SOLVED

just runned

json_last_error()

And it was an utf-8 encoding error

so... just used the solution proposed by Tiago here

Thanks everybody for the time you dedicated to me.

Have a nice day

Upvotes: 1

Related Questions