繁体   English   中英

在主IF外部打印数组值

[英]Printing array values outside main IF

我有一个问题,尝试解决了很长时间,但还是没有运气。 希望有人在这里帮助我。

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

这是我的验证码。

// 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);    
}

然后我试图从这个主要的IF条件外面打印$reg_errors数组,但是它没有打印它的值。 但是在主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>  

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM