无法将值分配给错误数组以处理PHP中的错误

时间:2019-07-04 09:13:00

标签: php arrays

我试图用PHP编写POST请求表格。用户提交表单时,代码应验证输入并将其放入称为错误的数组中。之后,系统检查该阵列是否为空。如果有东西,它将撞到一个警告框。

我面临的问题是无论我如何尝试分配值,错误数组仍然为空。下面是完整的代码。

<?php

    $errors = array('name' => '', 'age' => '' ); // array containing errors

    // check get request. The GET will be an array storing data to be transfered to server as GET. 
    // _ is global var. GET belows will take the value when the submit button pressed
    if(isset($_POST['submit'])){  // when user submit POST request
        errCheck();
        if(!empty($errors['name'])) {
            phpAlert($errors['name']);

        };   // check if errors exist
        if(!empty($errors['age'])) {
            phpAlert($errors['age']);

        };
    }

    function errCheck() {               // validate user input
        if(empty($_POST['name'])) {
            $errors['name'] = 'Name can\'t be emty';

        }
        elseif(!preg_match('/^[a-zA-Z\s]+$/', $_POST['name'])){ 
            $errors['name'] = 'Only letters available for name.';

        };
        if(empty($_POST['age'])) {
            $errors['age'] = 'Age can\'t be empty';

        } 
    }

    function phpAlert($msg){    // in case errors exist
        echo $msg;
        echo '<script type="text/javascript">alert("' . $msg . '")</script>';
    };

?>

<!DOCTYPE html> 
<html>
    <head>
        <title> my PHP </title>
    </head>

    <body>
        <h4 class="center">Add a Person</h4>
        <form class="white" action="15.php" method="POST">           <!-- GET request form -->
            <label>Name: </label>
            <input type="text" name="name">                            <!-- name is the key for http request -->
            <label>Age: </label>
            <input type="number" name="age">
            <div class="center">
                <input type="submit" name="submit" value="submit">
            </div>
        </form>


    </body>
</html>

预期结果:当用户在“名称”输入中未键入任何内容或未写任何字母时,出现警告框。

当前问题:代码可以运行,但是无论我在输入中键入什么内容,它始终会碰到2个空白警报框。错误数组始终为空。

我尝试过的(但不起作用):使错误变为全局数组,重新排列代码,一切都用print_r($ _POST有值,但没有$ errors),重新制作函数phpAlert。

已编辑:使代码更易于阅读。

2 个答案:

答案 0 :(得分:2)

在函数errCheck中定义错误并返回。

// check get request. The GET will be an array storing data to be transfered to server as GET. 
        // _ is global var. GET belows will take the value when the submit button pressed
        if(isset($_POST['submit'])){  // when user submit POST request

            $errors = errCheck();
            if(!empty($errors['name'])) {phpAlert($errors['name']);};   // check if errors exist
            if(!empty($errors['age'])) {phpAlert($errors['age']);};
        }

        function errCheck() {               // validate user input. Throw errors into errors array
            //Define the array of error within this function 
            $errors = array('name' => '', 'age' => '' ); // array containing errors
            if(empty($_POST['name'])) {$errors['name'] = 'Name can\'t be emty';}
            elseif(!preg_match('/^[a-zA-Z\s]+$/', $_POST['name'])){ $errors['name'] = 'Only letters available for name.';};
            if(empty($_POST['age'])) {$errors['age'] = 'Age can\'t be empty';} 

            //Return the errors
            return $errors;
        }

        function phpAlert($msg){    // in case errors exist
            echo $msg;
            echo '<script type="text/javascript">alert("' . $msg . '")</script>';
        };

答案 1 :(得分:0)

尝试使用此代码,您需要在函数 errCheck 中将 $ _ POST 作为参数传递,并返回错误数组。
      

                $errors = array('name' => '', 'age' => '' ); // array containing errors

                // check get request. The GET will be an array storing data to be transfered to server as GET. 
                // _ is global var. GET belows will take the value when the submit button pressed
                if(isset($_POST['submit'])){  // when user submit POST request
                   $errors= errCheck($_POST);
                    if(!empty($errors['name'])) {phpAlert($errors['name']);};   // check if errors exist
                    if(!empty($errors['age'])) {phpAlert($errors['age']);};
                }

                function errCheck($array) {               // validate user input. Throw errors into errors array
                   $errors=array();
                    if(empty($array['name'])) {$errors['name'] = 'Name can\'t be emty';}
                    elseif(!preg_match('/^[a-zA-Z\s]+$/', $array['name'])){ $errors['name'] = 'Only letters available for name.';};
                    if(empty($array['age'])) {$errors['age'] = 'Age can\'t be empty';} 

                   return $errors;
                }

                function phpAlert($msg){    // in case errors exist
                    echo $msg;
                    echo '<script type="text/javascript">alert("' . $msg . '")</script>';
                };

            ?>