php - Creating a perfect form with validation -


on website have plenty of form , fields, have system works painful , , running.

this system does, once send form info sent class possesses data , validates it. stores value of field $_session['userupdate']['firstname'][1] = $firstname;

if there error creates session variable $_session['userupdate']['firstname'][0] = 1; 1 tells field empty. if there no error session variable @ 0.

if no errors found in validation process data sent database.

after form page reloaded :

header( 'http/1.1 303 see other' ); header( 'location: '.curpageurl().'' );  

i use cannot resend data when reloading page. reason i'm using these session variables.

then lot of it/else if/else check values of session variables , output errors , populate form data entered previously.

let me show example firstname field.

this html code :

<label for="firstname" class="block">first name</label> <span>your first name goes here.</span><?php echo $text_first_name ?> <input type="text" id="firstname" class="mediatext" name="firstname" value="<?php echo $first_name2; ?>" onchange="needtoconfirm=true" <?php echo $style_first_name ?> /> 

this validation process class :

$_session['ui']['first_name'][1] = $this->first_name;  if (isset($this->first_name))      {         if($this->first_name == null)         {             $_session['ui']['first_name'][0] = 1;         }         else if(minrange(3, $this->first_name))          {                $_session['ui']['first_name'][0] = 2;          }         else         {             array_push($set, "firstname = '".$db->sql_escape($this->first_name)."'");         }     } 

this php code handles eventual errors :

$error_bg = "style=\"background:#ee5c42\"";  //first name if($_session['ui']['first_name'][0] == 1) {     $style_first_name = $error_bg;     $first_name2 = $_session['ui']['first_name'][1]; } else if($_session['ui']['first_name'][0] == 2) {     $style_first_name = $error_bg;     $first_name2 = $_session['ui']['first_name'][1];     $text_first_name = "<span class=\"errortext\">your first name must consist of @ least 3 characters.</span>"; } else {     $first_name2 = $userdetails["firstname"]; } 

at end of page there little function unset session variables.

i wondering there way make simpler , easier , running ?

if you're asking code optimization advice, here's how that:

// have bunch of error codes $possible_errors = array(   '0' => '', // no error   '1' => '', // error, no error message   '2' => 'your %s must consists of @ least 3 characters',   ... );  // have form fields stored in session // fields in session should named keys in $userdetails // 'formname' => array('firstname' => array(0, 'german'), 'lastname' => array('1', '')) $errors = $values = array(); foreach ($_session['formname'] $field => $element) {     if ($element[0] > 0) { // has error         $error_code = $element[0];         $error_message = $possible_errors[$error_code];         if (!empty($error_message)) {             $errors[$field] = sprintf($error_message, $field);         }     } else {         $values[$field] = $userdetails[$field];     } }  // in here end 2 arrays: //   - $errors error messages keyed field name //   - $values values keyed field name // use them <label for="firstname" class="block">first name</label> <span>your first name goes here.</span><?php if (array_key_exists('firstname', $errors)) echo $errors['firstname']; ?> <input type="text" id="firstname" class="mediatext" name="firstname"    value="<?php echo $values['firstname']; ?>"   onchange="needtoconfirm=true"   <?php if(array_key_exists('firstname', $errors)):?>style="background:#ee5c42"<?php endif;?> /> 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -