我有一个问题,并试图解决它很长一段时间但仍然没有运气。希望有人能在这里帮助我。
我正在尝试验证表单,在我的验证过程中,我使用数组来存储错误消息。然后我需要在验证码之外打印该数组。
这是我的验证码..
// 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>
答案 0 :(得分:0)
为什么在第二个代码块的开头添加global $reg_errors;
?我想你会从第一个代码块中覆盖$reg_errors
。
答案 1 :(得分:0)
从验证码创建一个函数(返回$ reg_errors)。 并将其称为您想要的地方。 不要使用全局变量。
如果它是同一个文件,请删除全局$ reg_errors,它应该可以工作。