如何使用ajax调用像codeigniter中的php函数?

This one is very simple. In codeigniter I can have an ajax call like:

    $.ajax({
        type: "POST",
        url: base_url + "index.php/mycontroller/myfunction"
       //so in mycontroller.php  ^ there is function myfunction ()
        data:{id : id},
        success:function(data){

         };
       })

Since class Mycontroller extends CI_Controller.
So how can I do that in raw PHP if I have posted.php, how can I extend this file in order for me to call a function like this:

    <?php
        function test(){
           echo 'Hello World!';
         }

What i'm thinking is like:

    $.ajax({
        type: "POST",
        url: "posted.php/test", //go to posted.php and call test function if possible
        data:{id : id},
        success:function(data){

         };
       })

But this one is not working. So any help?

You could change your ajax POST URL to something like this:

posted.php?call=test

then, in your posted.php, check the GET parameter 'call' and call the right function:

switch($_GET['call']){

  case 'test':
     test();
  break;
 }


 function test() {
     echo "Hello, world!";
 }

CodeIgniter uses some $_SERVER variables to be able to get this information for you. It actually can vary from environment to environment, but it is commonly in $_SERVER['PATH_INFO'] (some environments don't even support this, and CI has a fallback to use query params).

Try a print_r($_SERVER); to see if you have an PATH_INFO variable. From there, CI can use the string value determine the name of the function and call it.

Here's a simple example:

function test ()
{
    echo 'Test success!';
}

$fn = trim(@$_SERVER['PATH_INFO'], '/');

if (function_exists($fn)) call_user_func($fn);
else die ("No function: {$fn}");

Additional Info: From the CI source (application/config/config.php) regarding what it uses for its routing:

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO
|
*/