Return to Snippet

Revision: 42845
at March 11, 2011 08:28 by ddavidd


Updated Code
$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;
}

Revision: 42844
at March 11, 2011 08:28 by ddavidd


Initial Code
$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;
}

Initial URL
http://css-tricks.com/snippets/php/post-validation-loop/

Initial Description

                                

Initial Title
Post Validation Loop

Initial Tags

                                

Initial Language
PHP