php calls by ajax

Asked

Viewed 74 times

1

I have a PHP file that returns a Json.

This PHP file is called by Ajax, I have no functions in the PHP file. I receive the variables by POST in the PHP file...

Everything is working, but now I want to add functions in PHP and receive the data by parameter , at this time I am 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 have no classes or functions, in PHP I receive the parameters by $var= $_POST["key"];

Now I want to get the file up and running... how can I do that?

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);

}
  • If I understand correctly, you want to use functions with the parameters coming via POST, just take the variables assigned as you exemplified ($var= $_POST["key"]) and pass them to the functions. Post something you’ve done in this php file so we can help.

  • I’ll edit the code

1 answer

1

First you need to take 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 by passing these values. The file may look like this, but for the sake of organization, it is advisable that you separate the class into its own file.

$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);

    }
}

Obs.: Do not forget to process the data coming from the POST before call them in their class, using the functions filter_input

  • I didn’t know this filter_input

  • i want to have several functions in the same php file and call them by ajax sending parameters

  • @user2964140 is something similar to what you want? http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function

  • yes is this https://gist.github.com/jonsuh/3739844#file-Response-php-L12 found this example

Browser other questions tagged

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