tags:

views:

48

answers:

3

My script.php accepts $_POST input and echos a string.

$theinput = $_POST['theinput'];
$output = //some processing;
echo $output;

I need the same code in another function in a different file second.php and don't want to repeat the code. But the first script script.php expects input as $_POST. My input in the function is not $_POST just a regular parameter.

function processinput($someinput){
   //run the same script above, 
   //just the input is a parameter, not a `$_POST`
}

Any ideas how to do that? Is it possible to simulate a $_POST or something like that?

A: 
function processinput($someinput){ 
   //I need to call the above script and give it  
   //the same input `$someinput` which is not a `$_POST` anymore 
   $results = strrev($someinput);
   return $results;
} 

$theinput = $_POST['theinput'];
$output = processinput($theinput);
echo $output; 
Mark Baker
Thanks for the answer. But I'm not sure I understand the answer. The function and the script are not in the same file. Also the function is calling the `$_POST` script, not the other way around
donpal
-1: this makes no sense; reversing a string (`strrev`) has nothing to do with the question as far as I can tell.
JYelton
The strrev() is in there purely to demonstrate the function actually doing something, and something visible when viewing the result.... and note that this question has changed since I actually answered it
Mark Baker
Noting that it is for demonstrative purposes helps, thanks for clearing it up.
JYelton
+1  A: 

Are you looking for the include or include_once method?

second.php:

function processinput($someinput){
  $output = //some processing;
  echo $output;
}

script.php:

include_once('second.php'); // second.php contains processinput()
$theinput = $_POST['theinput'];
processinput($theinput);
Shiki
+3  A: 

you can always assign values to $_POST just as if it is an array. A bit of a hack-job, and you are probably better off changing the function to take the value in as a parameter, but it will work.

$_POST['theinput'] = "value";
Thomas Winsnes