1

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?

5
  • Are you wanting to call the test() function in posted.php? (I see by your edit that you do.) Commented Dec 13, 2013 at 13:21
  • @CWSpear Yes I am Sir Commented Dec 13, 2013 at 13:22
  • 1
    Well, you're gonna have to write your own framework that interprets the given URL and instantiates the controller and calls that function. Unless @Milanzor s method below is 'enough' for your application. Commented Dec 13, 2013 at 13:24
  • @AmazingDreams Yes Sir I just want to make it simple though. Commented Dec 13, 2013 at 13:25
  • extremely small and simple php framework you might want to look at is github.com/anandkunal/ToroPHP Commented Dec 13, 2013 at 13:27

2 Answers 2

6

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!";
 }
Sign up to request clarification or add additional context in comments.

5 Comments

Explanation: You cannot call functions by the HTTP request alone. PHP still need to determine what you want to do. This is why its required to have handlers like this answer points you to. So either adapt this code or implement your own.
Or, you can just do , $_GET['call']() .
@adirohan Yeah, if you want your user to be able to call all functions available... Which no one would want.
@adirohan I'd recommend against that, very unsafe, that way I can call any function you define
It wouldn't be so bad if every function in that file was dedicated to being able to be accessed in that manner. It could cut down on a potentially large and unnecessary switch. But if you have other functions you don't want accessed, then yes, absolutely $_GET['call']() would be a bad idea.
1

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
|
*/

1 Comment

Ah, you're on Windows. Are you at /posted.php/test in that dump? PATH_INFO won't show up at all if it would be otherwise empty. Really though, you are actually probably better off with @Milanzor's solution, as it's a better solution to solve the problem of AJAXing content, but I was trying to answer your specific question about how CI does it and how you could roll your own.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.