php calls by ajax

1

I have a PHP file that returns a Json.

This PHP file is called by Ajax, I do not have functions in the PHP file. I get the POST variables in the PHP file ...

This is all working, but now I want to add in PHP functions and receive the data by parameter, at the moment I'm doing so:

var pesquisa = $("#pesquisa").serialize();$.ajax({ 
 type: "POST",
    url: "../Logica/info/Getinfo.php",
    dataType: 'json',     
    data: pesquisa,

Of course, in the PHP file I do not have classes or functions, in PHP I get the parameters by $var= $_POST["key"];

Now I want to put the file with functions and keep it working ... How can I do this?

function getinfo($key,$query,$datainicio,$datafim,$op){

    if ($op != "" and ( $datainicio != "" and $datafim != "")) {
        $query = "$querys $op and (datainicio >:datainicio AND datafim<:datafim)";
        $db = new ligacao();
        $conn = $db->open();
        $stmt = $conn->prepare($query);
        $stmt->bindParam(':datainicio', $datainicio, PDO::PARAM_STR);
        $stmt->bindParam(':datafim', $datafim, PDO::PARAM_STR);
    }
        if ($key != "") {
        $query = "$querys  KEY=:KEY";
        $db = new ligacao();
        $conn = $db->open();
        $stmt = $conn->prepare($query);
        $stmt->bindParam(':KEY', $key, PDO::PARAM_STR);
    }
    $stmt->execute();
    $result = $stmt->fetchAll();
    $table = array();
    $rows = array();
    foreach ($result as $row) {
        $rows[] = $row;
    }
    $table['data'] = $rows;
    $json = json_encode($table);
    echo ($json);

}
    
asked by anonymous 23.09.2015 / 12:18

1 answer

1

First you need to get the data via POST, and then call your function with the captured values:

$key = $_POST["key"];

$datainicio = $_POST["datainicio"];

$datafim = $_POST["datafim"];

$op = $_POST[]"op"];

Then, just start an object of your class and call the method, passing these values. The file may look like this, but for the sake of organization, it is advisable that you separate the class into a file of your own.

$key = $_POST["key"];

$datainicio = $_POST["datainicio"];

$datafim = $_POST["datafim"];

$op = $_POST[]"op"];

$suaClasse = new SuaClasse();

$suaClasse->getinfo($key, $query, $datainicio, $datafim, $op);


class SuaClasse{

    function getinfo($key,$query,$datainicio,$datafim,$op){

        if ($op != "" and ( $datainicio != "" and $datafim != "")) {
            $query = "$querys $op and (datainicio >:datainicio AND datafim<:datafim)";
            $db = new ligacao();
            $conn = $db->open();
            $stmt = $conn->prepare($query);
            $stmt->bindParam(':datainicio', $datainicio, PDO::PARAM_STR);
            $stmt->bindParam(':datafim', $datafim, PDO::PARAM_STR);
        }
            if ($key != "") {
            $query = "$querys  KEY=:KEY";
            $db = new ligacao();
            $conn = $db->open();
            $stmt = $conn->prepare($query);
            $stmt->bindParam(':KEY', $key, PDO::PARAM_STR);
        }
        $stmt->execute();
        $result = $stmt->fetchAll();
        $table = array();
        $rows = array();
        foreach ($result as $row) {
            $rows[] = $row;
        }
        $table['data'] = $rows;
        $json = json_encode($table);
        echo ($json);

    }
}
  

Note: Do not forget to handle data coming from POST before   call them in your class using the filter_input functions

    
23.09.2015 / 13:06