类中对象的实例

时间:2013-06-28 03:43:16

标签: php forms class object

我有一个构建处理类的表单,我已经开发它来处理页面上的任何和所有表单处理。

当我在页面上有多个表单时遇到问题我正在使用类的实例构建每个表单:

    $form2 = new newForm('submitform2', 'post.contact.php', '100%');
$form2->setHandler('#canWe');
$form2->html('<div class="half">');
$form2->addText('name', 'Name:');
$form2->html('</div>');
$form2->html('<div class="halfNoMarg">');
$form2->addEmail('email_address', 'E-Mail Address:');
$form2->html('</div>');
$form2->html('<div class="half">');
$form2->addUrl('website', 'Website URL:');
$form2->html('</div>');
$form2->html('<div class="halfNoMarg">');
$form2->addText('phone', 'Phone Number:');
$form2->html('</div>');
$form2->addTextArea('about_business', 'Tell Us About Your Business:');

$form2->addSubmitButton('Find Out Now!');

$form2->getPost();
$form2->outputForm();

    $form = new newForm('submitform', 'post.contact.php', '100%');
$form->addText('name', 'Name:');
$form->addEmail('email_address', 'E-Mail Address:');
$form->addUrl('website', 'Website URL:');
$form->addText('phone', 'Phone Number:');
$form->addTextArea('about_business', 'Tell Us About Your Business:');

$form->addSubmitButton('Find Out Now!');

$form->getPost();
$form->outputForm();

当类处理表单并为缺少的字段等发出错误时,每个表单都会显示错误,而不仅仅是正在处理的表单对象。

基本上输出会影响类的每个实例,而不仅仅是正在处理的实例。

我可以包含类文件,如果有人需要帮助(它的650行,所以不想包含在帖子中)。

提前感谢您提供的任何帮助。

- M

在后期功能中添加编辑

//获取帖子数据,如果帖子通过了预检,则返回一个清理过的信息数组 公共函数getPost() {

//if post tests all passed then lets create the array of elements and load the processing file.
if ($this->checkPost() === true) {

    $cleanedPost = $this->cleanPost;

    //get the post processing file
    if (file_exists($this->processFile)) {
        require_once($this->processFile);
        //$this->postMessage($this->successMessage, 'confirm');
    } else {
        $this->postMessage('Processing File Not Found:<em>' . $this->processFile . '</em>', 'error');
    }

    return $this->cleanPost;

} else {

    return false;
}

}

//将表单输出到屏幕 public function outputForm() {     // echo spl_object_hash($ this);

if($this->handler !== null){
    $this->handler = 'action="'.$this->handler.'"';
}

$return = '<form id="' . $this->formName . '" method="' . $this->method . '"'. $this->handler .' enctype="multipart/form-data" style="width:' . $this->width . '" >';
if(!empty($this->message)){
    $return .= '<div class="errorMessage">' . $this->message . '</div>';
}
$return .= $this->output;
$return .= '</form>';

echo $return;

}

更多帖子处理代码

//在我们处理之前检查帖子 公共函数checkPost() {

//go through the entire post and process information as needed
if (!empty($_POST)) {

    foreach ($this->postCheck AS $postVar => $item) {

        if (is_array($item)) {

            //addItemTo cleanPost
            if(isset($_POST[$postVar])){
                $this->addToCleanedPost($postVar, $_POST[$postVar]);
            }else{
                $this->cleanPost[$postVar] = null;
            }

            switch ($item['type']) {

                //validate emails
                case 'email':
                    //check valid email
                    if (isset($this->cleanPost[$postVar]) && !$this->validEmail($this->cleanPost[$postVar])) {
                        $this->postMessage('Email Address is invalid');
                    }
                    break;

                //validate and verify password
                case 'password':
                    if ($item['verify'] === true) {
                        //validate password against other
                        if (!$this->verifyPassword($this->cleanPost['password'], $this->cleanPost['password2'])) {
                            $this->postMessage('Passwords do not match');
                        }
                    } else {
                        if (empty($this->cleanPost[$postVar]) && $item['req'] === 1) {
                            $this->postMessage($postVar . ': Please enter a password');
                        }
                    }
                    break;

                //validate checkboxes
                case 'checkbox':

                    if ($item['req'] === 1 && $this->validateCheckBox($postVar) === 0) {
                        $this->postMessage($postVar . ' must be checked');
                    } else {
                        $this->cleanPost[$postVar] = $this->validateCheckBox($postVar);
                    }
                    break;


                //handle url
                case 'url':
                    //first we need to handle making the url format properly
                    if(isset($this->cleanPost[$postVar])){
                        $url = $this->validUrl($this->cleanPost[$postVar]);

                        //now we do the final checking of the url and make sure it is a valid url before we move forwards
                        if ($item['req'] === 1 && $url === null) {
                            $this->postMessage($postVar . ' must be a valid URL');
                        }else{
                            $this->cleanPost[$postVar] = $url;
                        }
                    }

                    break;

                //handle text and text areas
                case 'text':
                case 'textarea':

                    if (isset($_POST[$postVar])) {

                        //check for a bypass on the min required characters
                        if (isset($item['bypass']) && $item['bypass'] === true || $this->minBypass === true) {

                            $bypassMinCheck = true;

                        } else {

                            $bypassMinCheck = false;
                        }

                        //process the item
                        if ($item['req'] === 1 && $bypassMinCheck === false && strlen($this->cleanPost[$postVar]) < 2) {

                            $this->postMessage($postVar . ' is too short');

                        } else {

                            if (!empty($this->cleanPost[$postVar]) && $bypassMinCheck === false) {

                                if (strlen($_POST[$postVar]) < 2) {

                                    $this->postMessage($postVar . ' is too short');

                                }

                            }
                        }
                    }
                    break;

                //handle other inputs
                default:
                    if (empty($_POST[$postVar]) && $item['req'] === 1) {
                        $this->postMessage($postVar . ' cannot be left empty');
                    }
                    break;
            }

        }
    }
} else {
    return false;
}

//return the success or failure of the prepost test
if (empty($this->message)) {
    return true;
} else {
    return false;
}

}

0 个答案:

没有答案
相关问题