开发者

AJAX working with a php class

I am starting to learn about classes in php and was wondering how I would be able to call a function that is within a class, but using it in the url field for ajax.

Basically, I have:

$(document).ready(function(){
    $('#submit').click(function(e) {
        $.ajax({
            type : 'POST',
            url : 'addCustomer.php',
            dataType : 'json',
            data:{
                lastName : $('#add_LN').val(),
                firstName : $('#add_FN').val(),
                phone : $('#add_PN').val(),
                dob : $('#add_DOB').val()开发者_JAVA技巧
            },

What my addCustomer.php does is create an instance of my class "customer_module" and then calls functions within customer_module.

But I would like to get rid of addCustomer.php all together, and just call the function in customer_module.php. This functions name will be addCustomer($last_name, $first_name, $phone, $dob).

customer_module.php

    class customer_module
    {

        public function addCustomer($last_name, $first_name, $phone, $dob)
        {
...
        }

     }

What should I change my ajax function to look like?


You can't do this, you always need a wrapper around it. There are PHP Frameworks like Kohana (derived from CodeIgniter), which do this kind of mapping URLs to functions and classes but this is very difficult and in your case it would be better to keep on using the addCustomer.php solution.


Directly and as such, I don't know of a way to directly call a member of a class from AJAX. There pretty much must always be a wrapper of some sort. That said, it is fairly easy to write the wrapper so that it will take the name of the function to call as a parameter, therefore allowing you to call multiple functions using just a single wrapper function. If you do this, I strongly suggest that you only allow it to access a specified list of functions (to prevent certain types of cyber attacks).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜