php - Dynamic if-statement with variables? -


i'm trying create dynamic if-statement. reason want this, because need check server-sided whether inputfields match regex , not empty. however, of inputfields can removed in cms, meaning there more/less inputfields accordingly.

ideally add variables in if-statement i'm not 100% sure if that's allowed, perhaps need other way solve problem. here's tried:

if ($f_naw['streetname'] == 1) {     $streetname= $_post['streetname']; //used in insert query     $cstreetname = " || $_post['streetname'] == ''"; //used check if field empty     $pstreetname = " || !preg_match($streetnamereg,$_post['streetname'])"; //used check if matches regex } else {     //these variables define variables if inputfields not shown     $streetname= ''; //no streetname means it's excluded in insert query     $cstreetname = ''; //not needed in check     $pstreetname = ''; //also not needed in check }  // more of these if/else statements  if ($_post['firstname'] == '' || $_post['lastname'] == '' || $_post['email'] == '' $cstreetname $cpostalcode $chometown $ctelnr $csex $cdateofbirth) {     echo 'one of fields empty.';     header('refresh:3;url=index.php'); } else {     //regex check, after more code } 

my idea check if specific field shown on front-end , in case i'm creating variables want paste in if-statements.

i'm getting error saying server error meaning php-code invalid.

is possible @ make dynamic if-statement? if yes, @ part failing?

help appreciated! in advance.

first of all, since looks need combine of conditionals ||, can correct program writing this:

if ($f_naw['streetname'] == 1) {     $streetname= $_post['streetname']; //used in insert query     $cstreetname = $_post['streetname'] == ''; //used check if field empty     $pstreetname = !preg_match($streetnamereg,$_post['streetname']); //used check if matches regex } else {     //these variables define variables if inputfields not shown     $streetname= ''; //no streetname means it's excluded in insert query     $cstreetname = false; //not needed in check     $pstreetname = false; //also not needed in check }   if ($_post['firstname'] == '' || $_post['lastname'] == '' || $_post['email'] == '' || $cstreetname || $cpostalcode || $chometown || $ctelnr || $csex || $cdateofbirth) {     echo 'one of fields empty.';     header('refresh:3;url=index.php'); } 

this work, it's unwieldy. better solution use array (let's name $errors gets dynamically populated errors resulting validating fields. this:

$errors = array();  if ($f_naw['streetname'] == 1) {     $streetname= $_post['streetname']; //used in insert query     if ($streetname == '') {         $errors[] = 'streetname cannot empty.'; // message optional     }     if (!preg_match($streetnamereg,$streetname)) {         $errors[] = 'streetname invalid.'; // message optional     } } 

and then:

if ($errors) {     echo 'there errors data submitted.';     header('refresh:3;url=index.php'); } 

if provided human-readable error messages can arrange them displayed user knows need fix. , of course there lots of variations of technique can use -- e.g. group error messages field show 1 error each field.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -