您好我是php新手并测试了一些代码我得到了一个未定义函数的错误
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" placeholder="type your full name" name="fullname">
<input type="submit" name="submit" value="submit">
</form>
<?php
$getName = "" ;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$getName = "hello ".test_input($_POST['fullname']); //Fatal error: Call to undefined function test_input() in C:\xampp\htdocs\ctPHP.php on line 15
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
echo $getName;
?>
</body>
</html>
我想知道如何解决这个问题,我想在文本字段中回显出键入的名称,如何解决这个问题
答案 0 :(得分:1)
只需要在创建函数后回显。
首先创建函数test_input()
然后在声明/定义后使用它。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" placeholder="type your full name" name="fullname">
<input type="submit" name="submit" value="submit">
</form>
<?php
$getName = "" ;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$getName = "hello ".test_input($_POST['fullname']); //Fatal error: Call to undefined function test_input() in C:\xampp\htdocs\ctPHP.php on line 15
}
echo $getName;
?>
</body>
</html>