2

I want to be able to submit a value from a form and then call the function submitted, my code:

<?php
include 'specs.php';
if (empty($_POST) === false) {
$func = $_POST['function'];
$a1 = $_POST['a1'];
$a2 = $_POST['a2'];
$a3 = $_POST['a3'];
$a4 = $_POST['a4'];
$a5 = $_POST['a5'];
echo $func.'('$a1', '$a2', '$a3', '$a4', '$a5');';
}
?>
<form action="" method="post">
Function:<br><input type="text" name="function"><hr>
Value 1: <input type="text" name="a1">
Value 2: <input type="text" name="a2">
Value 3: <input type="text" name="a3">
Value 4: <input type="text" name="a4">
Value 5: <input type="text" name="a5">
<input type="submit" value="Execute">
1
  • Hopefully that's behind some kind of login or you whitelist the allowed function calls. Commented Apr 26, 2013 at 2:18

4 Answers 4

4

You can use call_user_func_array

call_user_func_array ( 'thefuncname' , your_parameters )

In your case it would be something like:

$func = $_POST['function'];
$parameters = array($a1, $a2, $a3, $a4, $a5);
call_user_func_array ( $func, $pameters);
Sign up to request clarification or add additional context in comments.

Comments

1

Or just echo $func(array($a1, $a2, $a3, $a4, $a5));

(maybe you don't want an array, so you can just put $func($a1, $a2, $a3, etc);)

Comments

0

replace

echo $func.'('$a1', '$a2', '$a3', '$a4', '$a5');';

with

call_user_func_array($func, array($a1,$a2,$a3,$a4,$a5));

1 Comment

@Evan By the looks of what OP tried to echo, seems like those are parameters indeed. Anyway, +1 for your answer.
0
//suggust add some prefix on func ,then check function_exists
$func = 'auto_'.$_POST['function'];
if (function_exists( $func)) {
 $func(array($a1, $a2, $a3, $a4, $a5));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.