POST validation loop

POST validation loop

Online by JSC0d3
June 16, 2014 | | 919 recognitions

POST Validation Loop

This code starts by creating an array that holds the name of various inputs being submitted via a POST.

getFormData() is then called, where the required fields are passed in. Inside the function an array is created to hold various pieces of data related to the form. $formData[‘valid’] is a boolean referencing if all data was provided and valid, $formData[‘fields’] is an array keyed by the name of the input with their respective value from the POST data, $formData[‘notValidFields’] is an array that will contain the names of any inputs that were not passed or that had non-valid data.

$requiredFields = array('field1', 'field2', 'field3', 'field4');
$formData = getFormData($requiredFields);

function getFormData($requiredFields){
   $formData = array();
   $formData['valid'] = true;
   $formData['fields'] = array();
   $formData['notValidFields'] = array();

   for($a = 0; $a < count($requiredFields); $a++){
       $field = $requiredFields[$a];
       if(isset($_POST[$field])){
           $value = $_POST[$field];
           if(empty($value)){
                $formData['valid'] = false;
                $formData['notValidFields'][] = $field;
           }else{
                $formData['fields'][$field] = $value;
           }
       }else{
           $formData['valid'] = false;
           $formData['notValidFields'][] = $field;
       }
   }
   return $formData;
}
JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | February 9, 2019

WordPress' wp_link_pages() function, used for displaying page links in multi-page posts, lacks one big feature You can display a list of...

Posted by | December 14, 2018

Preliminary step: Testing whether the code complies with the format of a fiscal identification code (CIF) That is, the maximum length should be 10...

Posted by | November 8, 2017

Nowadays, a lot of bloggers choose to remove HTML tag from WordPress websites However, for beginners and newbies in this field, they may feel...

Previous PostBackNext Post

Leave here an impression