Calling PHP function in a class

Asked

Viewed 423 times

2

Good afternoon, I am creating a simple PHPOO system with MVC and would like to run a function inside the controller with a jquery call. I don’t want to call the controller in html, I want a js to do this job for me. I tried to do it this way: My controller is in controller/Userscontroller. Class:

<?php
class UserController{
   public function teste(){
       echo "ok";
   }
}
?>

and the JS:

$("#btn-login").click(function(){
        var data = $("#login-form").serialize();

        $.ajax({
            type : 'POST',
            url  : 'UserController.php', 
            data : data,
            dataType: 'json',
            beforeSend: function()
            {   
                $("#btn-login").html('Validando ...');
            },
            success :  function(response){                      
                if(response.codigo == "1"){ 
                    $("#btn-login").html('Entrar');
                    $("#login-alert").css('display', 'none')
                    window.location.href = "home.php";
                }
                else{           
                    $("#btn-login").html('Entrar');
                    $("#login-alert").css('display', 'block')
                    $("#mensagem").html('<strong>Erro! </strong>' + response.mensagem);
                }
            }
        });
    });

});

I want to run the test function within the Class.

Grateful for the help.

  • 1

    Making a request for the file that just defines the class makes no sense. You need to have in PHP the instantiation of the class and the execution of the desired method. Javascript will not be able to instantiate and call a PHP method automatically.

  • I had the same problem a while ago, the solution I found was to add an attribute inside the post (that this sent) with the name of the function passed, then created a boundary class that has no function, which serves to check the post, and call the php function. I know it’s not the answer you’re waiting for, but if you don’t find a solution it’s like a tip, I’ll keep an eye on your question, the answers can help me too.

  • Thanks for the reply, I found this tutorial https://luis4raujo.wordpress.com/2015/08/24/development-com-php-orienteda-object-usando-a-architecture-mvc-e-requisicao-ajax/.

No answers

Browser other questions tagged

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