PHP致命错误:在第11行的C:\ wamp \www \web \ new9.php中调用未定义的函数test_input() [英] PHP Fatal error: Call to undefined function test_input() in C:\wamp\www\web\new9.php on line 11

查看:189
本文介绍了PHP致命错误:在第11行的C:\ wamp \www \web \ new9.php中调用未定义的函数test_input()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


致命错误:调用未定义的函数test_input()in第11行的C:\wamp\www \web \ new9.php

The following form shows an error on clicking the submit button if we enter the name only and submit.The error shown is

Fatal error: Call to undefined function test_input() in C:\wamp\www\web\new9.php on line 11

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


推荐答案

将您的功能移出条件如果声明

Move your function out of your conditional if statement

它应该是这样的......

It should be like this...

<?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致命错误:在第11行的C:\ wamp \www \web \ new9.php中调用未定义的函数test_input()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆