2017-06-14 66 views
0

我是新來的php OOPPHP:服務器端單選按鈕驗證

目前我使用下面的代碼來驗證服務器端的窗體。

了在此情況下使用的文件是:

register.php:

<form action="" method="post"> 
      <input type="text" placeholder="first name" value="<?php echo Input::get('fname'); ?>" name="fname" autocomplete="off" /> 
      <input type="text" placeholder="last name" value="<?php echo Input::get('lname'); ?>" name="lname" autocomplete="off" /> 
      <input type="email" placeholder="email" value="<?php echo Input::get('email'); ?>" name="email" autocomplete="off" /> 
      <input type="password" placeholder="choose password" value="<?php echo Input::get('password'); ?>" name="password" autocomplete="off" /> 
      <input type="password" placeholder="confirm password" value="<?php echo Input::get('password_again'); ?>" name="password_again" autocomplete="off" /> 
      <span class="gen-sel"> 
       <input type="radio" name="gen" value="male" id="male" /> 
       <label for="male" class="fa fa-male"> male</label> 
       <input type="radio" name="gen" value="female" id="female" /> 
       <label for="female" class="fa fa-female"> female</label> 
      </span> 
      <button class="btn right" type="submit" name="submit_register">Register</button> 
      <span>Already signed up? <a href="login.php">click here</a> to login</span> 
     </form> 

Input.php這是一種類檢查的輸入是否已經提交與否:

<?php 

class Input 
{ 
    public static function exists($type = 'post'){ 
     switch($type){ 
      case 'post': 
       return (!empty($_POST)) ? true : false; 
       break; 

      case 'get': 
       return (!empty($_GET)) ? true : false; 
       break; 

      default: 
       return false; 
       break; 
     } 
    } 

    public static function get($item){ 
     if(isset($_POST[$item])){ 
      return $_POST[$item]; 
     } else if(isset($_GET[$item])){ 
      return $_GET[$item]; 
     } 

     return ''; 
    } 
} 

最後一個文件是Validate.php,它是一個具有驗證代碼的方法(它對於文本輸入字段,例如電子郵件,文本,密碼正常工作):

public function check($source, $items = array()){ 
    foreach ($items as $item => $rules){ 
     foreach ($rules as $rule => $value){ 
      $value = $source[$item]; 
      if($rule === "required" && empty($value)){ 
       $this->addError($item,"Marked field is required"); 
      } else{ 

      } 
     } 

     if(empty($this->_errors)){ 
      $this->passed = true; 
     } 
    } 

    return $this; 
} 

當我提交我碰到下面的錯誤給我的文本框的形式:

注意:未定義指數:根在/用戶/森/網站/工作區/禪卡斯特/班/ Validation.php第16行

指向朝這行代碼在Validation.php:

$value = $source[$item]; 

我知道,代替鄰f選定的代碼我在這裏添加了大量的代碼,但我真的不能理解這裏的錯誤!

感謝您的幫助提前

+0

你能告訴我你怎麼調用檢查方法和什麼是$源,$ items –

回答

1

要檢查的數組中的索引 - 這是正確的 - 但你這樣做嘗試使用它之後。你應該首先檢查:

if($rule === "required" && empty($source[$item])) { 
+0

謝謝,它的工作原理 – sameer

0

你可以嘗試var_dump$source,那裏你可以看看有什麼是$source陣列英寸

因爲錯誤導致做這樣的事實,價值是否存在使用empty功能無法找到$source