변수에 값을 입력하고 배열에 변수 값을 넣은 다음 양식을 유효하게하려고 시도하고 있습니다. 유효하지 않은 필드에 대해 오류 메시지를 출력합니다. 채워 넣습니다. 두 가지 문제가 있습니다. 첫째, if 문은 필드가 비어 있거나 == 'undefined'인 경우에도 모든 값에 대해 실행되고 두 번째로 변수 값 대신 변수의 실제 이름을 출력하는 방법을 알지 못합니다. 배열을 사용하여 PHP 폼 유효성 검사 - 변수 이름 인쇄
print_x($variable)//prints 'variable' instead of 'hello'
$variable = 'hello';
예를 들어 I는 다음과 같이 두 가지 방법을 시도했다.
$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']);
감사
지금까지 첫 번째로
개인적으로, 저는 is_numeric이 과학 표기법, 16 진수 등을 받아들이 기 때문에 ctype_digit 또는 is_numeric 대신에 사용합니다 (이것은 폼의 유효성을 검사 할 때 대부분의 사람들이 찾고있는 것이 아닙니다). – preinheimer