Php form validation with an array - print the name of a variable -


I try to validate the post value and value variables by inserting those variables into an array and cycling through them. I am here. Output error messages for fields that are not filled. I have two problems. First of all, if the statement is going on for all values, even if the field is empty or == is 'undefined' and the other thing is not to say that I am going to the actual name of a variable instead of a variable How to print For example

  $ variable = 'hello';  

print_x ($ variable) // print is 'variable' instead of 'x'

I have tried two methods Which are shown below.

  $ error_message = "should fill in the following fields: 

And there is a different way where I use variable variables

  $ Error_message = "should fill in the following fields: & lt; br / & Gt; "; $ Fields_to_validate_arr = array ('category', 'productive', 'product_name', 'image_name', 'description', 'stock_content', 'miss_sl'); Foreach ($ fields_to_validate_arr $ v) {if (empty ($$ v)} $$ v = 'undefined') {// variable bariables // Increase error message with custom message error $ Error_message = "- & gt;" . Ucwords (str_replace ('_', '', $ v)).  

The variable is forwarded to my code such as

  $ range = myescape_function ($ _ POST ['category']);  

Thanks

There is no need to create your input variable Array, because you already have $ _POST:

  $ _ POST = array_map ('myescape_function', $ _POST); Foreach ($ fields_to_validate_arr $ v) {if (empty ($ _ POST [$ v]) $$ _POST [$ v] == 'undefined') {// Increase error message with a custom error message $ Error_message . = "- & gt;" . Ucwords (str_replace ('_', '', $ v)). "& Lt; br / & gt;"; }}  

Since the values ​​are not stored in separate variables, hence the problem of printing the name of a variable is removed rather than the name of the value.

If you really want to get fancy, you can add support for custom assumptions:

  function inputExists ($ name, & source $) { Come back! Below ($ source [$ name]) & amp; Amp; 'Undefined'! = $ Source [$ name]; } Function Input In Numeric ($ name, & source =) {return input accustomed ($ name, $ source) & amp; Amp; Is_numeric ($ source [$ name]); } // America's phone numbers only check the input insphon ($ name, and $ 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, and $ source, $ RE) {return inputExists ($ name, $ source) & amp; Amp; Preg_match ($ RE, $ source [$ name]); } Function name ANDValidator ($ name, $ validator) {if (function_exists ($ validator)} {return array ('name' = & gt; $ name, 'validator' = & gt; $ validator, 'data' = & Gt; ''); } Elseif (is_numeric ($ name)) // If the index is numeric, then assume $ validator actually returns the name return value ('name' = & gt; $ validator, 'validator' =>, 'inputExists',' Data '= & gt;' '); } Other {Return Array ('name' = & gt; $ name, 'validator' = & gt; 'inputMatchesRE', 'data' = & gt; $ validator); }} $ Fields_to_validate_arr = Array ('name', 'street' = & gt; '/ ^ \ d + + [az] + (ay | st | y | way | ln | lp | blvd) $ / i', ' Age '= & gt;' inputIsNumeric ',' phone '= & gt;' inputIsPhone '); $ _POST = array_map ('myescape_function', $ _POST); Foreign currency ($ name = & gt; $ validator as $ fields_to_validate_arr) {list ($ name, $ validator, $ data) = nameAndValidator ($ name, $ validator); If (! Call_user_func ($ validator, $ name, $ _POST, $ data)) // error message increases with a custom error message $ Error_message = "- & gt;" . Ucwords (str_replace ('_', '', $ v)). "& Lt; br / & gt;"; }}  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -