Hi, I am trying to do form validation by putting the post values into variables and those variables into an array and then cycling through them and outputting error messages for fields that are not filled in. I am having two issues. Firstly, the if statement is running for all the values even if the field is empty or is == to 'undefined' and secondly i don't know how to print out the actual name of a variable instead of the variable value. For example
$variable = 'hello';
print_x($variable)//prints 'variable' instead of 'hello'
I have tried two methods which are shown below.
$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
}
}
And a different method where i 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
}
}
The variables are assigned further up in my code like
$category = myescape_function($_POST['category']);
Thanks