2

我希望能够从表单提交一个值,然后调用提交的函数,我的代码:

<?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">
4

4 回答 4

4

您可以使用call_user_func_array

call_user_func_array ( 'thefuncname' , your_parameters )

在您的情况下,它将类似于:

$func = $_POST['function'];
$parameters = array($a1, $a2, $a3, $a4, $a5);
call_user_func_array ( $func, $pameters);
于 2013-04-26T02:16:55.933 回答
1

要不就echo $func(array($a1, $a2, $a3, $a4, $a5));

(也许你不想要一个数组,所以你可以放$func($a1, $a2, $a3, etc);

于 2013-04-26T02:19:30.880 回答
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));
}
于 2013-04-26T03:19:16.680 回答
0

代替

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

call_user_func_array($func, array($a1,$a2,$a3,$a4,$a5));
于 2013-04-26T02:18:43.153 回答