How to call a specific PHP function with AJAX?

0

I need to call a specific function inside a PHP file with AJAX. I have tried in many ways, I have even done research here in Stack , but it does not roll at all.

What I'm doing:

PHP code:

$functionName = filter_input(INPUT_GET, 'functionName');

if (isset($_GET['functionName']) && !empty($_GET['functionName'])) {

    $functionName = $_GET['functionName'];

    switch ($functionName) {
        case 'getInscritos': 
            getInscritos(); 
        break;
    }

}

public function getInscritos() {

$datas = array();

    $vagas = get_post_meta($this->id, InscricoesPostType::ID . '_vagas');
    $ins = get_post_meta($this->id, InscricoesPostType::ID . '_inscritos');

    foreach ($data as $key => $value) {
        $datas[] = array(
            'vagas' => $vagas[$key],
            'inscritos' => $ins[$key]
        );
    }

    return json_encode(get_post_meta($this->id, InscricoesPostType::ID . '_vagas'));
}

JS Code:

$.ajax({
                type: "GET",
                url: '/../dir1/dir2/dir3/dir4/arquivo.php',
                data: "functionName=getInscritos",
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                success: function(data) {
                    console.log('Função encontrada' + data);
                },
                error: function(data){ 
                    var obj = jQuery.parseJSON(data);
                    console.log('Função não encontrada: ' + obj); 
                }
            });
    
asked by anonymous 27.11.2017 / 13:59

0 answers