Validate variable of type ajax in cakephp 3

0

You will see I have three variables in my javascript function that is as follows:

function search_person_report(){
var User_persons = $("#email_person").val();
var names = $("#names").val();
var lastnames = $("#lastnames").val();
console.log(name);
$.ajax({
    type: 'POST',
    async: true,
    cache: false,
    url: person_report,
    dataType: 'json',
    data: {User_persons:User_persons,names:names,lastnames:lastnames},
    success: function (data, textStatus, jqXHR) {
        console.log(data);
        var result_person = "";
        $.each(data,function(i,item){
            result_person +='<tr>';
            result_person += '<td>'+item.usuario+'</td>';
            result_person += '<td>'+item.Nombre+'</td>';
            result_person += '<td>'+item.Apellidos+'</td>';
            result_person += '<td>'+item.fecha_creacion+'</td>';
            result_person += '<td><input type="checkbox" value="'+item.id_persona+'" name="id_person[]"></td>';
            result_person +='</tr>'
        });
        $("#resultperson").html(result_person);
    }
});

}

well there is no problem, the issue is that these variables I have validate them, that is, if they are empty or not within the function in cakephp and then pass it to the SP which will send me the data to show them, here the code cakephp

public function searchpersongroup() {
    $IdUser = $this->Auth->user('id_usuario');
    $user = $this->request->query('User_persons');
    $names = $this->request->query('names');
    $lastnames = $this->request->query('lastnames');
    if (!empty($user)) {
        $user = '%'.$user;
    }else{
        $user = null;
    }
    if (!empty($names)) {
        $names = '%'.$names;
    }else{
        $names = null;
    }
    if (!empty($lastnames)) {
        $lastnames = '%'.$lastnames;
    }else{
        $lastnames = null;
    }
    $connection = ConnectionManager::get('default');
    $query_search = $connection->execute('CALL ser_search_persons_rpt_grupal(:_IdUser,:_User,:_Names,:_Lastnames)',[
        ':_IdUser'=> $IdUser,':_User'=> $user, ':_Names' => $names,':_Lastnames' => $lastnames])->fetchAll('assoc');
    if ($this->request->is('ajax')) {
        echo json_encode($query_search);
        die();
    }
}

When I do the test it shows me empty, I do not know what is wrong doing help please

    
asked by Jonathan Cunza 11.05.2017 в 23:31
source

1 answer

0

Variables that are sent to a CakePHP controller from an AJAX call are not received in the variable $ this- > request- > query but in the global variable $ _POST.

So that your variables will be the following:

    $user = $_POST['User_persons']);
    $names = $_POST['names']);
    $lastnames = $_POST['lastnames']);

Learn more

This way you should correctly recover the variables passed in the controller.

Greetings and I hope it serves you!

    
answered by 04.08.2017 в 11:34