[英]Not displaying values from an array
我有一個包含此代碼的表單,因此在檢查類的字段時可以回顯錯誤:
<?php if( isset($_POST['send'])){
$new_user = new Register();
$new_user->check_required_fields($_POST);
$new_user->display_errors();
}
?>
該類是:
<?php
class Register extends Database
{
public $fname;
public $lname;
public $uname;
public $email;
public $pass1;
public $pass2;
public $year;
public $month;
public $day;
public $required_array;
public $error;
public $errors = array();
public function check_required_fields($required_array)
{
if(in_array('', $required_array)) {
$errors[] = "One or more fields are missing";
//var_dump($errors);
}
else
{
$errors[] = "All fields are ok";
$this->fname = $required_array['fname'];
$this->lname = $required_array['lname'];
$this->uname = $required_array['lname'];
$this->email = $required_array['email'];
$this->pass1 = $required_array['pass1'];
$this->pass2 = $required_array['pass2'];
$this->year = $required_array['year'];
$this->month = $required_array['month'];
$this->day = $required_array['day'];
}
}
public function display_errors ($errors)
{
foreach ($errors as $error){
echo $error;
}
}
由於某種原因,它將不會顯示$ errors數組,而且我不確定為什么嗎? 謝謝,我將不勝感激。
嘗試使用
$this->errors
在check_required_fields和display_errors中。
public function display_errors ($errors)
{
foreach($errors as $error){
echo $error;
}
}
在foreach語句中使用的“ $ errors”是函數display_errors的參數列表中的一個,當您調用該函數時,您沒有給出任何參數,因此此變量將為空
您應該在foreach語句中使用$ this-> errors
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.