PHP fatal error : Call to undefined function test_input() in C:\wamp\www\web\new9. php 第 11 行

标签 php html function if-statement error-handling

如果我们只输入名称并提交,下面的表单显示单击提交按钮时出错。显示的错误是

fatal error :在第 11 行调用 C:\wamp\www\web\new9.php 中未定义的函数 test_input()

谁能在下面的代码中找到问题。

      <?php
// define variables and set to empty values
$name1Err = $email1Err  =  "";
$name1 = $email1 =  "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
 if (empty($_POST["input_1"]))
    {$name1Err = "Your name is required. Just the first will do. ";}
  else
    {$name1 = test_input($_POST["input_1"]);
     if (!preg_match("/^[a-zA-Z ]*$/",$name1))
       {       $name1Err = "Only letters and white space allowed"; }
    }

 if (empty($_POST["input_12"]))
    {$email1= "";}
  else
    {$email1 = test_input($_POST["input_12"]);
    // check if e-mail address syntax is valid
     if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email1))
       {  $emailErr = "Invalid email format"; }
    }




function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
}
?>
        <form method='post' enctype='multipart/form-data' action=''>
                name<input name='input_1' type='text' value='<?php echo $name1;?>' tabindex='1' />
                <div class="validation_message"><?php echo $name1Err ?></div>

                email<input name='input_12'  type='email' value='<?php echo $email1;?>'/><br/>
                <textarea name='input_5' tabindex='9'   rows='10' cols='50'></textarea>

            <input type='submit' value='Submit' tabindex='25' />

    </form>


<?php
echo "<h2>Your Input:</h2>";
echo "Name:".$name1;
echo "<br>";
echo "Email:".$email1;
echo "<br>";
?>

最佳答案

将函数移出条件 if 语句

应该是这样的……

<?php
// define variables and set to empty values
$name1Err = $email1Err  =  "";
$name1 = $email1 =  "";

// Moved here
function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// .... your remaining code .......... !

来自 PHP 文档...

When a function is defined in a conditional manner ... Its definition must be processed prior to being called.

Source

关于PHP fatal error : Call to undefined function test_input() in C:\wamp\www\web\new9. php 第 11 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21139420/

相关文章:

php - PHP 中的 Laravel header 和缓存

jquery - 如何删除 jQuery DataTables 插件添加的搜索栏和页脚?

javascript - 使用对象方法设置 html 值真的很奇怪

php - 在 PHP 中从 mysql 检索图像时无法查看图像

r - 在 R 中将包名称作为参数传递

python - 一般编程问题。什么时候使用 OOP?

python - 1 作为函数声明中的参数类型

php - 处理 CASE 语句中的 NULL

php - 通过 laravel 连接 Postgresql 数据库的问题

php - 可迭代对象和数组类型提示?