Wednesday, 27 April 2016

Handling Post Request in PHP

Instead of $_POST you can handle the post data using function. By using this you need not to worry about the undefined index notice.
function postData($key) {
    $res = '';
    if (isset($_POST[$key])) {
        if (!is_array($_POST[$key])) {
            $res = htmlentities($_POST[$key]);
        } else if (is_array($_POST[$key]) && !empty($_POST[$key])) {
            $res = array();
            foreach ($_POST[$key] as $k => $v) {
                $res[$k] = htmlentities($v);
            }
        }
        return $res;
    }
    return '';
}

No comments:

Post a Comment