tags:

views:

59

answers:

7

hi! this might sound weird :)

i want to "call" a file( test_2.php ) i.e execute the file like how it would have been when clicking on the following link:

<a href="home/test_2.php">click here</a>  //this code is currently in test_1.php file

is there any method to do that?

+4  A: 

Are you possibly looking for include()?

http://php.net/manual/en/function.include.php

Kyle
A: 

You may want include()?

http://php.net/manual/en/function.include.php

<?php
  include("test2.php"); //This is just like inserting test2.php's code in this file
  /* rest of the code */
?>
Vinko Vrsalovic
A: 

I'm having trouble understanding your answer, especially the link part, but do you perhaps mean including files http://php.net/manual/en/function.include.php?

Joe
+1  A: 

I think Curl is what you want.

Darrell Brogdon
A: 

If your server supports it, include() using a http:// URL should do the trick and produce exactly the desired result.

If you include a filesystem path (e.g. include /home/sites/www.example.com/test.php) the file would be parsed in the current context which may not be what you want.

Pekka
A: 

Do you mean run a script and catch it's output? For that I use the following:

    /**
    *       Execute a php file and return it's output.
    *       The file will be executed in the script's context. 
    *       It is not sandboxed in any way and it can access the global scope.
    *
    *       @param  string  $file           The file to execute.
    *       @param  array   &$context       An optional array of name=>value 
    *                       pairs to define additional context.
    *       @return string  The file's output.
    */
    function execFile($file, &$context=NULL)
    {
            if(is_array($context)) 
            {
                    extract($context, EXTR_SKIP);
            }

            ob_start();
            include $file;
            $ret = ob_get_contents();
            ob_end_clean();
            return $ret;
    }

Be careful what you give to this function.

Dinu Florin
+1  A: 

I think what you mean is, when you click the link, you want that php to get executed, but you don't want the page to change/reload.

Ajax is your friend here. If you are using jquery, just one line of javascript code will do it:

$.get('home/test_2.php', function(data, txt){ alert(txt); });

read more about this function at jQuery.get documentation.

Cheers!

Here Be Wolves
Very good guess, harshanth.jr!
Joe
i'm a teacher at heart :D
Here Be Wolves