2009-12-05 43 views
0

我想通過將發佈的值放入變量和這些變量到數組中,然後循環遍歷它們並輸出錯誤消息填寫。 我有兩個問題。首先,即使字段爲空或者==爲'未定義',if語句也會運行所有值,其次我不知道如何輸出變量的實際名稱而不是變量值。例如PHP數組的表單驗證 - 打印一個變量的名稱

$variable = 'hello'; 

print_x($variable)//prints 'variable' instead of 'hello'

我試圖將在下面示出了兩個方法。

$error_message = "The following fields must be filled in:<br />"; 
     $fields_to_validate_arr = array($category,$producer,$product_name,$image_name,$description,$stock_quantity,$min_sale); 
     foreach($fields_to_validate_arr as $v){ 
      if(empty($v) || $v = 'undefined'){//using variable bariables 
       //increment the error message with a custom error message. 
       $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables 
      } 
     } 

和不同的方法,在這裏我使用可變變量

$error_message = "The following fields must be filled in:<br />"; 
    $fields_to_validate_arr = array('category','producer','product_name','image_name','description','stock_quantity','min_sale'); 
    foreach($fields_to_validate_arr as $v){ 
     if(empty($$v) || $$v = 'undefined'){//using variable bariables 
      //increment the error message with a custom error message. 
      $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables 
     } 
    } 

的變量是在我的代碼進一步上漲分配了類似的

$category = myescape_function($_POST['category']); 

感謝

回答

1

沒有必要創建自己的輸入變量數組,既然你已經有了$ _ POST:

$_POST = array_map('myescape_function', $_POST); 
foreach($fields_to_validate_arr as $v){ 
    if(empty($_POST[$v]) || $_POST[$v] == 'undefined'){ 
     //increment the error message with a custom error message. 
     $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />"; 
    } 
} 

由於值沒有存儲在獨立的變量,輸出變量的名字的問題而不是它的價值消失。

如果你想獲得真正看中的,您可以添加支持自定義驗證:

function inputExists($name, &$source) { 
    return !empty($source[$name]) && 'undefined' != $source[$name]; 
} 
function inputIsNumeric($name, &$source) { 
    return inputExists($name, $source) && is_numeric($source[$name]); 
} 
// checks for U.S. phone numbers only 
function inputIsPhone($name, &$source) { 
    if (inputExists($name, $source)) { 
     // strip whatever non-numeric 
     $value = preg_replace('/[-.,() \t]+/', '', $source[$name]); 
     return preg_match('^(1?[2-9]\d{2})?[2-9]\d{6}$', $value); 
    } 
    return False; 
} 
function inputMatchesRE($name, &$source, $RE) { 
    return inputExists($name, $source) && preg_match($RE, $source[$name]); 
} 
function nameAndValidator($name, $validator) { 
    if (function_exists($validator)) { 
     return array('name' => $name, 'validator' => $validator, 'data' => ''); 
    } elseif (is_numeric($name)) { 
     // if index is numeric, assume $validator actually holds the name 
     return array('name' => $validator, 'validator' => 'inputExists', 'data' => ''); 
    } else { 
     return array('name' => $name, 'validator' => 'inputMatchesRE', 'data' => $validator); 
    } 
} 

$fields_to_validate_arr = array('name', 'street' => '/^\d+ +[a-z ]+(ave|st|wy|way|ln|lp|blvd)$/i', 'age'=> 'inputIsNumeric', 'phone' => 'inputIsPhone'); 

$_POST = array_map('myescape_function', $_POST); 

foreach($fields_to_validate_arr as $name => $validator){ 
    list($name, $validator, $data) = nameAndValidator($name, $validator); 
    if(! call_user_func($validator, $name, $_POST, $data)){ 
     //increment the error message with a custom error message. 
     $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />"; 
    } 
} 
+0

就個人而言,我會使用ctype_digit或類似的東西,而不是is_numeric,因爲is_numeric接受科學記數法,十六進制等等(這不是大多數人在驗證表單時要查找的東西)。 – preinheimer 2009-12-06 00:32:48

1

至於你的第一個代碼塊去你的IF語句中有一個錯誤。您正在設置$ v ='undefined'這將每一次評估爲真。您需要爲IF語句使用相等運算符。

$error_message = "The following fields must be filled in:<br />"; 
    $fields_to_validate_arr = array($category,$producer,$product_name,$image_name,$description,$stock_quantity,$min_sale); 
    foreach($fields_to_validate_arr as $v){ 
     if(empty($v)){ //using variable variables 
      //increment the error message with a custom error message. 
      $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables 
     } 
    } 
+0

我無法看到它們,它們是什麼? – andrew 2009-12-05 23:46:28

+0

Oh im missing the == – andrew 2009-12-05 23:47:08

+0

@andrew - 編輯並解釋 – 2009-12-05 23:47:12