0

我有一个问题,并试图修复它很长时间,但仍然没有运气。希望有人会在这里帮助我。

我正在尝试验证表单,在验证过程中我使用数组来存储错误消息。然后我需要从我的验证代码中打印该数组。

这是我的验证码..

// For storing registration errors:
$reg_errors = array();

// Check for Write Testimonial form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Check for Name:
    if (preg_match ('/^[A-Z \'.-]{2,80}$/i', $_POST['name'])) {
        $name = mysqli_real_escape_string ($dbc, $_POST['name']);
    } else {
        $reg_errors['name'] = 'Name : This field is required.';
    }

    // define a constant for the maximum upload size
    define ('MAX_FILE_SIZE', 1048576 );

    if (array_key_exists('testimonial_image', $_POST)) {

        // Create a temporary file name:
        $file = time() . '-' . $_FILES['testimonial_photo']['name'];

        // convert the maximum size to KB
        $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';

        // create an array of permitted MIME types
        $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');

        // begin by assuming the file is unacceptable
        $sizeOK = false;
        $typeOK = false;

        // check that file is within the permitted size
        if ($_FILES['testimonial_photo']['size'] > 0 && $_FILES['testimonial_photo']['size'] <= MAX_FILE_SIZE) {
            $sizeOK = true;
        }

        // check that file is of an permitted MIME type
        foreach ($permitted as $type) {
            if ($type == $_FILES['testimonial_photo']['type']) {
                $typeOK = true;
                break;
            }
        }

        if ($sizeOK && $typeOK) {
            switch($_FILES['testimonial_photo']['error']) {
                case 0:

                // check if a file of the same name has been uploaded
                if (!file_exists(UPLOAD_DIR.'/'.$file)) {
                    // move the file to the upload folder and rename it
                    $success = move_uploaded_file($_FILES['testimonial_photo']['tmp_name'], UPLOAD_DIR.'/'.$file);
                } 

                if (!$success) {
                    $reg_errors[] = "Error uploading $file. Please try again.1";
                    echo "Error uploading $file. Please try again.1";
                }
                break;
                case 3:
                    $reg_errors[] = "Error uploading $file. Please try again.2";
                    echo "Error uploading $file. Please try again.2";
                    exit(); // Quit the script.                         

                default:                    
                    $reg_errors[] = "System error uploading $file. Contact webmaster.";                     
                    echo "System error uploading $file. Contact webmaster.";                        
            }

        } elseif ($_FILES['testimonial_photo']['error']) {      
                        $reg_errors[] = 'No file selected';                 
                        echo 'No file selected';                    
        } else {                
            $reg_errors[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: GIF, JPEG, PNG.";         
            echo  "$file cannot be uploaded. Maximum size: $max. Acceptable file types: GIF, JPEG, PNG.";           
        }

        echo $file;
    }

    // Delete the file if it still exists:
    if (file_exists ($_FILES['testimonial_photo']['tmp_name']) && is_file($_FILES['testimonial_photo']['tmp_name']) ) {
        unlink ($_FILES['testimonial_photo']['tmp_name']);
    }
    //print_r( $reg_errors);    
}

然后我试图在$reg_errors这个主要条件之外打印数组,IF但它没有打印它的值。但主要IF是打印它的值。

<div class="group">

    <!-- Display Error Messages -->
    <?php
        global $reg_errors; 
        print_r( $reg_errors); 

        if(!empty($reg_errors)) {
            echo '<div class="error">
                        <img src="images/error.png" />
                        <h3>Errors found in this form</h3>
                        <h4>Whoops! - There is a problem with the form, please check and correct the following.</h4>
                        <ul>';
                foreach( $reg_errors AS $error ) {
                    echo "<li>$error</li>";
                                            }

            echo '   </ul>
                    </div>';                                    
        }
?>  
</div>  
4

2 回答 2

0

为什么要global $reg_errors;在第二个代码块的开头添加 ?我猜你$reg_errors从你的第一个代码块覆盖你的。

于 2013-02-07T07:53:15.800 回答
0

从您的验证代码中创建一个函数(返回 $reg_errors)。并在你想要的地方调用它。不要使用全局变量。

如果是同一个文件,删除全局 $reg_errors,它应该可以工作。

于 2013-02-07T07:53:39.123 回答