duanguan1573 2018-09-10 07:00
浏览 89

如何使用函数在php中实现ajax

I would like to build a web app using json, ajax and php to be more clear, I need a php page with different functions that can be called from ajax method

Eg: phppage.php

addmydetails() 
{
   logic goes here
}

ajax page

/* Get from elements values */
 var values = $(this).serialize();

 $.ajax({
        url: "phppage.php/addmydetails",
        type: "post",
        data: values ,
        success: function (response) {
           // you will get response from your php page (what you echo or print)                 

        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }


    });

So my actual question is that any option to call the function like this in ajax post url: "phppage.php/addmydetails"

  • 写回答

1条回答 默认 最新

  • drvonr6573 2018-09-10 07:17
    关注

    No need for URL rewriting. Just get the PATH and use call_user_func. Something like this:

    In your phppage.php:

    $method = trim($_SERVER["PATH_INFO"], "/");
    
    if(function_exists($method)){
        call_user_func($method);
        exit();
    }
    
    function Your_function_name(){
        print "this is my function";
    }
    

    Then you can access it using this URL --> phppage.php/Your_function_name

    评论

报告相同问题?