Sam
Sam

Reputation: 349

calling and executing a file in php

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?

Upvotes: 0

Views: 147

Answers (7)

jrharshath
jrharshath

Reputation: 26583

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!

Upvotes: 1

Daniel Dinu
Daniel Dinu

Reputation: 1793

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.

Upvotes: 0

Pekka
Pekka

Reputation: 449385

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.

Upvotes: 0

Darrell Brogdon
Darrell Brogdon

Reputation: 6973

I think Curl is what you want.

Upvotes: 1

Joe
Joe

Reputation: 47609

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?

Upvotes: 0

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340171

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

Upvotes: 0

Kyle
Kyle

Reputation: 4366

Are you possibly looking for include()?

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

Upvotes: 4

Related Questions