我想通过将发布的值放入变量和这些变量到数组中,然后循环遍历它们并输出错误消息填写。 我有两个问题。首先,即使字段为空或者==为'未定义',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']);
感谢
就个人而言,我会使用ctype_digit或类似的东西,而不是is_numeric,因为is_numeric接受科学记数法,十六进制等等(这不是大多数人在验证表单时要查找的东西)。 – preinheimer 2009-12-06 00:32:48